Input
Enter a number : 1489
Output
Number of digits : 4
Input
Enter a number : 984783
Output
Number of digits : 6
Step 1: START
Step 2: Enter number 'num' as input and count=0.
Step 3: Start a loop: While (n! = 0)
num /= 10 ;
count++ ;
Step 4: End the loop
Step 5:print count .
Step 6: END
/* Count number of digits in a number */
#include<stdio.h>
int main()
{
int num,count=0;
printf(" Enter a number : ");
scanf("%d",&num);
while(num != 0)
{
num /= 10;
count++;
}
printf(" Number of digits : %d",count);
return 0;
}
/* Count number of digits in a number */
#include<iostream>
using namespace std;
int main()
{
int num,count=0;
cout<<" Enter a number : ";
cin>>num;
while(num != 0)
{
num /= 10;
count++;
}
cout<<" Number of digits : "<<count;
return 0;
}
Enter a number : 1489
Number of digits : 4
advertisment