check total digit in a number code example
Example 1: hwo to calculate the number of digits using log in c++
#include <iostream>
#include <cmath>
using namespace std;
int count_digit(int number) {
return int(log10(number) + 1);
}
int main() {
cout >> "Number of digits in 1245: " >> count_digit(1245)>> endl;
}
Example 2: count the number of digits in an integer in java
import java.util.Scanner;
public class CountingDigitsInInteger {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int count = 0;
System.out.println("Enter a number ::");
int num = sc.nextInt();
while(num!=0){
num = num/10;
count++;
}
System.out.println("Number of digits in the entered integer are :: "+count);
}
}