Description

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 of 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;
}

Output:

Enter the Number : 1882
Total Digits : 4

Algorithm of count the digit in a number.

Step 1: SET n and count = 0

Step 2: READ n

Step 3: REPEAT Step 4 and Step 5 while n>0

Step 4: n = n/10

Step 5: count = count + 1

[End Loop]

Step 6: PRINT count

Step 7: EXIT

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Insertion and Deletion of all operation at Singly Linked List in C Programming Language

Insertion and Deletion in Singly(singular) linked list in C programming langauge Data Structure. How to Implement insertion and deletion.

Simple Macro Substitution(#define) in C and C++ programming language

Program to Explain Simple Macro Substitution ( #define ) c and c++ programming language

Print numbers from 1 to 100 using while loop c and cpp program

C program to print numbers from 1 to 100 using while loop and also c++ program.

How to make swastik in c and cpp programming language

Write a c and c++ program to print the Swastik. How to make Swastik in c and cpp.

User Login System in C programming language

We create a login System program which takes username and password from the user.

Program to print a message without using semicolon in c programming

How to write a c program to print a message without using semicolon in c programming language.