Header Ads

Digits Counting of a Number

Digits counting of a give number in C: -

Logic for digit counting : let number be 743 then
743 / 10 yields reminder as 3
which is count by counter i as i = 1
Now number is 74
74 / 10 yields reminder as 4
now counter i= 1
so, i= i+1
i = 2
similarly
i= i +2
i= 3
now print the i

condition is (do till number is greater then 0)

Program

#include<stdio.h>
#include<conio.h>
main()
{
 int num;
 printf("Enter the number :\n");
 scanf("%d",&num);
 void numcount(int);
 numcount(num);
 getch();
 
}
void numcount(int num1)
{
 int r,c=0;
 while(num1>0)
 
 {
  r=num1%10;
  num1=num1/10;
  c=c+1;
 }
 printf("No. of digit = %d",c);
}

Output

Enter the number :
759
No. of digit = 3_

No comments