Write a C program to count total number of digits of an Integer number (N).

Write a C program to count the total number of digits of an Integer number (N).In this post will be discussed how to write a program to count the total number of digits of an integer number (N). 

So at first, we initialize N as integer and then take and store input value in N using scanf() and again we initialize two-variable named  count=0 and num=N.  Then we have used while loop and inside the while loop, we had put the condition num>0  

Now if you don't know how to use while loop you should read these posts.

Inside the while loop, we have used two statements that will be executed num=num/10; and  count++; and then we have used printf() statement to print the answer i.e how many digits it is contained in the number. if you don't know the use of printf() function then you can check out this post 
Rough run:
Now for an example, if we give input 570 then it will be stored into N and the program will initialize count = 0 and num = 570 then the program control will go inside the while loop as the condition num>0 i.e 570 >0 is a true and inside the while loop 
Now for the first iteration Num=N and count = 0  then it will execute the statement num=num/10 ; 
i.e num= 570/10 and i.e equal to 57 so after execution of the statement above the value of num became 57. Then the program will execute the next statement i.e   count++; now after execution, the value of count became 1.

For the second iteration the condition of the while loop again true as num=57 i.e >0 
so the program control will go inside the loop and again the statement num=num/10 ; will be executed i.e num= 57/10 and i.e equal to 5 as it will take only an integer value so after the execution of the statement above the value of num became 5 and similarly, the count value will increase and now count =2.

For the third iteration the condition of the while loop again true as num=5 i.e >0 
so the program control will go inside the loop and again the statement num=num/10 ; will be executed i.e num= 5/10 and i.e equal to 0 as it will take only an integer value so after the execution of the statement above the value of num became 0 and the count value will increase and now count=0.

For the 4th iteration, the condition of the while loop became false as because num = 0 so this time it will not enter in the loop and it will execute the next statement i.e 
printf("The number %d contains %d digits.",N,count); and the program will print
 ' The number 570 contains 3 digits ' and hence the program execution will be completed.

This is the code for Writing a C program to count the total number of digits of an Integer number (N).


#include <stdio.h>

 int main()

{

    int N; 

    scanf("%d",&N);

   int count=0,num;
   num=N;
   while(num>0)
{
  num=num/10;
      count++;
 
}

printf("The number %d contains %d digits.",N,count);

}



 Input                          Output
570
 The number 570 contains 3 digits.


Conclusion

Hence the problem of how to write a program to count the total number of digits of an integer number is solved. If you have any question regarding this post then feel free to comment down below we will definitely try to answer it.

You also check out these posts also

Post a Comment

0 Comments