Previous | Home | Next |
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.
/* 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(); }
Enter a number 12
Digit is =2
Previous | Home | Next |