Learn C with example
Input a Number and Count its Digit
In this example we are going to find number of digits
into a number.
We are using while loop to find the number of
digits into a given number .We just find modulo of number and reduce the last
digit from original number .While modulo of number is greater than zero
then increase value of p .Finally when the modulo is zero. Then while
loop will end and the value of p is equals to number of digits of that
number.
Source Code Of the Example
/* input a number and count its digit*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,p=0;
clrscr();
printf("Enter a number");
scanf("%d",&i);
while(i!=0)
{
i=i/10;
p++;
}
printf("Digit is =%d",p);
getch();
}
|
Output Of Example
Enter a number12
Digit is =2 |