program to count the digits of the number in c-programming?
Now ,we will look for how to count the number of the digits enter by the user,which is as an integer. solution are following:-
In this program takes an integer form the user and count the digits of the number.
for example: if the user enter 4638.the output of the program will be 4.
#include<stdio.h>
#include<conio.h>
main()
{
int x,count=0;
printf("enter a number");
scanf("%d",&x);
while(x!=0)
{
x=x/10;
count++;
}
printf("\ntotal number is %d",count);
getch();
}
The integer is entered by the user is stored in variable in x .Then the while loop is iterated until the test expression x!=0 is evaluated to 0 (false).
1. after the iteration. the value of x will by 457 and count is incremented to 1.
2 After second iteration, the value of x will be 45 and count is incremented to 2.
3 After the third iteration .the value of the x will be 4 and the count is increment to
4 At the fourth iteration , the value of the x will be 0 and the count is terminated to
No comments:
Post a Comment