- To print total number of digits in a number
User Input: 15
Output:
Total number of digits : 2
import java.util.Scanner;
public class CountDigits {
public static void main(String []args) {
int num,count=0;
Scanner userInput = new Scanner(System.in);
System.out.print("Enter Your number : ");
num = userInput.nextInt();
while(num!=0) {
num/=10;
count++;
}
System.out.println("Total number of digits : "+count);
}
}
Enter Your number : 1132 Total number of digits : 4Program
Main Logic :
while(num!=0){
num/=10; // num=num/10
count++;
}
Divide the number by 10 while your number becomes 0[ZERO]
& Just count how many times it divides the number to make it ZERO
Coming Soon !
QuickPractice isn’t the thing you do once you’re good. It’s the thing you do that makes you good.