➤ If-else Statement in C
➤ Programs on if-else
➤ Switch Case in C
➤ Switch case Programs
➤ Conditional Operator
➤ While loop in C
➤ Do-while loop in C
➤ While vs do-while
➤ For loop in C
➤ Break keyword in C
➤ Continue keyword in C
➤ Break vs Exit in C
➤ Goto keyword in C
☕️ Flow Control Programs
➤ Largest in 3 Numbers
➤ Find Grade of student
➤ Find the absolute value
➤ Vowel or Consonant
➤ Leap Year Program
➤ Simple calculator in C
➤ Check Odd or Even
➤ Roots of Quadratic Equation
➤ Find Reverse of Number
➤ Factors of a number in C
➤ Generate Multiplication table
➤ Find Power of a Number
➤ Find GCD and LCM
➤ Find factorial of Number
➤ Count Number of Digits
➤ Sum of digits in Number
➤ Sum of N Natural Numbers
➤ Sum of Squares of Natural No.
➤ Find Sum of Odd Numbers
➤ Find the Sum of Series
➤ Find Fibonacci series in C
➤ Sum of the Fibonacci series
➤ Sum until enters +ve numbers
➤ Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
➤ Celsius to Fahrenheit
➤ Fahrenheit to Celsius
➤ Decimal ↔ Binary
➤ Decimal ↔ Octal
➤ Octal ↔ Binary in C
☕️ Number Programs in C
➤ Prime Number in C
➤ Strong Number in C
➤ Krishnamurthy Number
➤ Neon Number in C
➤ Palindrome number
➤ Perfect Number in C
➤ Armstrong Number
☕️ Pattern Programs in C
➤ Pattern programs in C
➤ Printing pattern using loops
➤ Floyd’s triangle Program
➤ Pascal Triangle Program
➤ Pyramid Star Pattern in C
➤ Diamond Pattern Programs
➤ Half Diamond pattern in C
➤ Print Diamond Pattern
➤ Hollow Diamond Pattern
➤ Diamond Pattern of Numbers
Write a C program to count the number of digits in an integer using for loop, while loop, without using recursion, and using recursion.
If we take the number 985632, it has 6 digits. For count number of digits, declare a count variable and initialize it with 0. Divide the number by 10 and then increase the count value, repeat this process. When the number becomes 0 then stop the process.
Prerequisites:- While loop in C, For loop in C
Count the Number of Digits in an Integer using the while loop
#include<stdio.h>
int main()
{
int number, count=0;
printf("Enter a number: ");
scanf("%d",&number);
while (number!=0)
{
count++;
number /= 10;
}
printf("Number of digits = %d\n",count);
return 0;
}
Output:-
Enter a number: 12586
Number of digits = 5
Using for loop
The for loop decrease the lines of code used in the previous program. The initialization, condition, and update expression are placed at a single place so for loop make program cleaner.
#include<stdio.h>
int main()
{
int number, count;
printf("Enter a number: ");
scanf("%d",&number);
for(count=0; number!=0; number /= 10)
{
count++;
}
printf("Number of digits = %d\n",count);
return 0;
}
C Program to Count the Number of Digits in an Integer without using a loop
We can use strlen()
function to count the number of digits in an integer without using the loop. The strlen() function is defined under “string.h” header file and it returns the number of characters of a string.
#include<stdio.h>
#include<string.h>
int main()
{
int number;
char ch[50];
printf("Enter a number: ");
scanf("%d",&number);
sprintf(ch, "%d", number);
printf("Number of digits = %ld\n", strlen(ch));
return 0;
}
Count the Number of Digits in an Integer using Recursion
Instead of using a pre-defined function, we can write our own function to count the number of digits in an integer. Below numberOfDigits()
is a recursive function, which calls itself when the number is not equal to zero.
Learn more about:- recursive function and C programs using the recursive function
#include<stdio.h>
int numberOfDigits(int n)
{
return n==0? 0 : 1+numberOfDigits(n/10);
}
int main()
{
int number, sum;
printf("Enter a number: ");
scanf("%d",&number);
sum = numberOfDigits(number);
printf("Number of digits = %d\n",sum);
return 0;
}
The recursive function numberOfDigits()
returns 0 if the number is 0, else it calls itself from the expression 1+numberOfDigits(n/10)
. In every call, the number is divided by zero and then it is passed as an argument.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Also See:-