PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to count number of digits in a number

Input - Output

Input
Enter a number : 1489

Output
Number of digits : 4

Input
Enter a number : 984783

Output
Number of digits : 6

Algorithm

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

CODE


/* 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;
 }

OutPut

Enter a number : 1489

Number of digits : 4