You're online now.

Hurray! you are online now.

Write a program to count the digit in a number

To Count the digits of a given number, divided that number by 10 until that number is greater than 0.

For each iteration, divide that number by 10 until the number is greater than 0 and increment the count variable by 1. For Example : 1923 → There are 4 digits.

// C program to count of the digits.
    #include<stdio.h>
    int main(){
        
        int n, count = 0;
        printf("Enter the Number : ");
        scanf("%d", &n);
        
        while(n>0){
            n = n/10;
            count = count + 1;
        }
        
        printf("Total Digits : %d", count);
        return 0;
    }
// C++ program to count the digits.
    #include<iostream>
    using namespace std;
    int main(){
        
        int n, count = 0;
        cout<<"Enter the Number : ";
        cin>>n;
        
        while(n>0){
            n = n/10;
            count = count + 1;
        }
        
        cout<<"Total Digits : "<<count;
        return 0;
    }
  1. SET n and count = 0.
  2. READ n.
  3. REPEAT step 4 and step 5 while n > 0.
  4. count = count + 1.
    [END LOOP]
  5. print count.
  6. EXIT
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...