In this post, we will learn how to count number of digits in an integer using the C Programming language.
This program will take an integer from the user and read the integer and count the total number of digits in the integer. For example: If the user enters the integer 5464, then the output will be 4.
So, without further ado, let’s begin the tutorial.
C Program To Count Number of Digits in an Integer
// C Program To Count Number of Digits in an Integer #include <stdio.h> int main(){ int num; // Asking for Input printf("Enter an Integer: "); scanf("%d", &num); //logic int count = 0; while (num > 0){ count++; num /= 10; //can also write num = num / 10 } printf("Total Number of Digits are: %d", count); return 0; }
Output
Enter an Integer: 1829
Total Number of Digits in 1829 are: 4
How Does This Program Work ?
int num;
In this program, we have declared a variable names as num.
// Asking for Input
printf("Enter an Integer: ");
scanf("%d", &num);
Then, the user is asked to enter the integer. The value of the integer will be stored in the num variable.
//logic
count = 0;
while (num > 0){
count++;
num /= 10;
}
- After the first iteration, the value of the num will be 182 and the count is incremented to 1.
- After the second iteration, the value of the num will be 18 and the count is incremented to 2.
- After the third iteration, the value of the num will be 1 and the count is incremented to 3.
- After the fourth iteration, the value of the num will be 0 and the count is incremented to 4.
- Then, the test expression of the loop is evaluated false and the loop terminates.
Some of the used terms are as follow:
#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.
The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.
Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.
scanf() – scanf() function is used to take input from the user.
printf() – printf() function is used to display and print the string under the quotation to the screen.
while Loop – In the while loop, the statements inside the body of the while loop keeps executing until it is evaluated to False.
// – Used for Commenting in C.
C Program To Count Number of Digits Using User Defined function
// C Program To Count Number of Digits in an Integer #include <stdio.h> int countDigits(int num) { int count = 0; while (num > 0){ count++; num = num / 10; } return count; } int main() { int num, a; // Asking for Input printf("Enter an Integer: "); scanf("%d", &num); // logic a = countDigits(num); printf("Total Number of Digits in %d are %d.", num, a); return 0; }
Output
Enter an Integer: 12345
Total Number of Digits in 12345 are 5
C Program To Count Number of Digits Without Using Loops
// C Program To Count Number of Digits in an Integer Without Using Loops #include <stdio.h> #include <math.h> int main(){ int num; int count = 0; // Asking for Input printf("Enter an Integer: "); scanf("%d", &num); // logic if (num > 0) { count = (num == 0) ? 1 : (log10(num) + 1); printf("Total Number of Digits in %d are %d", num, count); } else{ printf("Enter Positive Integer\n"); } return 0; }
Output
Enter an Integer: 8484985
Total Number of Digits in 8484985 are 7
Conclusion
I hope after going through this post, you understand how to count the number of digits in an integer using C Programming language.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
=====================
“Well_hello_there!”
=====================
How’ve been, long time no see?
Love to speak longer but I’m little busy!
GoodBye!