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;
}
Comments
Oops!