Approach
- Firstly, the number will be entered by the user. Suppose we declare the variable 'n' and stores the integer value in the 'n' variable.
- We will create a while loop that iterates until the value of 'n' is not equal to zero.
- Suppose the value of 'n' is 123.
- When the first iteration executes, the value of 'n' will be 123, and the value of count will be incremented to 1.
- When the second iteration executes, the value of 'n' will be 12, and the value of count will be incremented to 2.
- When the third iteration executes, the value of 'n' will be 1, and the value of count will be incremented to 3.
- After the completion of the third iteration, the value of 'n' becomes 0, and loop is terminated as it does not satisfy the condition (n!=0).
Let's develop a software that will execute the aforementioned strategy.
#include <stdio.h>
int main()
{
int n; // variable declaration
int count=0; // variable declaration
printf("Enter a number");
scanf("%d",&n);
while(n!=0)
{
n=n/10;
count++;
}
printf("\nThe number of digits in an integer is : %d",count);
return 0;
}
Output
Enter a number
The number of digits in an integer is : [value]
Next, we will explore the method of determining the quantity of digits without the reliance on a loop structure.
We can determine the quantity of digits by utilizing the logarithmic function. The count of digits is obtained by employing the formula log10(num) + 1, where log10 is a built-in function in the math.h library.
Let's see a simple example.
#include <stdio.h>
#include<math.h>
int main()
{
int num; // variable declaration
int count=0; // variable declaration
printf("Enter a number");
scanf("%d",&num);
count=(num==0)?1:log10(num)+1;
printf("Number of digits in an integer is : %d",count);
return 0;
}
Output
Enter a number
The number of digits in an integer is : [value]
We are going to develop a C program that utilizes functions to calculate the total count of digits.
#include <stdio.h>
int main()
{
int num; // variable declaration
int count=0; // variable declaration
printf("Enter a number");
scanf("%d",&num);
count=func(num);
printf("Number of digits is : %d", count);
return 0;
}
int func(int n)
{
int counter=0; // variable declaration
while(n!=0)
{
n=n/10;
counter++;
}
return counter;
}
Output
Enter a number
The number of digits in an integer is : [value]
Now, we will explore the process of recursively determining the count of digits.
#include <stdio.h>
int main()
{
int num; // variable declaration
int count=0; // variable declaration
printf("Enter a number");
scanf("%d",&num);
count=func(num);
printf("Number of digits is : %d", count);
return 0;
}
int func(int n)
{
static int counter=0; // variable declaration
if(n>0)
{
counter=counter+1;
return func(n/10);
}
else
return counter;
}
In the provided code snippet, we are determining the count of digits in an integer. The func method is invoked for this purpose. Inside func, a static variable named counter is defined, ensuring it is initialized just once. The recursive call func(n/10) continues until the condition (n>0) is met. Upon reaching the false condition, the function returns the value stored in the counter.
Output