Write a Java Program to Count Number of Digits in a Number using For Loop, While Loop, Functions, and Recursion
Java Program to Count Number of Digits in a Number using While Loop
This Java program allows the user to enter any positive integer and then it will divide the given number into individual digits and count those individual digits using Java While Loop.
// Java Program to Count Number of Digits in a Number using While Loop package SimplerPrograms; import java.util.Scanner; public class NumberofDigitsUsingWhile { private static Scanner sc; public static void main(String[] args) { int Number, Count=0; sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); Number = sc.nextInt(); while(Number > 0) { Number = Number / 10; Count = Count + 1; } System.out.format("\n Number of Digits in a Given Number = %d", Count); } }
Within the following statements, System.out.println statement will print the statement inside the double Quotes and next we are assigning the user entered value to integer variable (Number)
System.out.println("\n Please Enter any Number: "); Number = sc.nextInt();
Next line, we used the Java While Loop and the Condition inside the While loop will make sure that, the given number is greater than 0 (Means Positive integer)
while(Number > 0) { Number = Number / 10; Count = Count + 1; }
In this Java Program to Count Number of Digits in a Number example, User Entered value: Number = 1465 and we initialized the Count = 0
First Iteration
Number = Number / 10 = 1465 / 10
Number = 146
Count = Count + 1 = 0 + 1
Count = 1
Second Iteration
Within the first Iteration, Number = 146 and Count = 1
Number = 146 / 10
Number = 14
Count = 1 + 1
Count = 2
Third Iteration
Within the Second Iteration, both Number and Count’s value changed as Number = 14 and Count = 2
Number = 14 / 10
Number = 1
Count = 2 + 1
Count = 3
Fourth Iteration
Within the third Iteration, both Number and Count’s values changed as Number = 1 and Count = 3.
Number = 1 / 10
Number = 0
Count = 3 + 1
Count = 4
Here, Number = 0. So, the condition present in the while loop will fail
The last Java System.out.format statement prints the number of digits in that number.
System.out.format("\n Number of Digits in a Given Number = %d", Count);
The output of given variable 1465 is 4
Java Program to Count Number of Digits in a Number using For Loop
This Java program allows the user to enter any positive integer and then it will divide the given number into individual digits and then count those individual digits using Java For Loop.
// Java Program to Count Number of Digits in a Number using For Loop package SimplerPrograms; import java.util.Scanner; public class NumberofDigitsUsingFor { private static Scanner sc; public static void main(String[] args) { int Number, Count=0; sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); Number = sc.nextInt(); for (Count = 0; Number > 0; Number = Number/10) { Count = Count + 1; } System.out.format("\n Number of Digits in a Given Number = %d", Count); } }
We just replaced the While loop in the above example with the For loop.
Java Program to Count Number of Digits in a Number Using Functions
In this Java program we are using the same steps that we followed in our second example but we separated the logic and placed in the separate method.
// Java Program to Count Number of Digits in a Number using Functions package SimplerPrograms; import java.util.Scanner; public class NumberofDigitsUsingFunctions { private static Scanner sc; private static int Count = 0; public static void main(String[] args) { int Number; sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); Number = sc.nextInt(); Count = NumberofDigits(Number); System.out.format("\n Number of Digits in a Given Number = %d", Count); } public static int NumberofDigits(int Number) { for (Count = 0; Number > 0; Number = Number/10) { Count = Count + 1; } return Count; } }
If you observe the following statement, we are calling the NumberOfDigits (Number) method and assigning the return value to integer variable Count.
Count = NumberofDigits(Number);
When the compiler reaches to NumberOfDigits (Number) line in the main() program, the Javac immediately jump to the below function:
public static int NumberofDigits(int Number) {
Java Program to Count Number of Digits in a Number Using Recursion
It allows the user to enter any positive integer and then it will divide the given number into individual digits and counting those individual digits using Java Recursion concept.
In this Java program example, we are dividing the code using OOPS. To do this, First, we will create a class that holds a method to count the number of digits in a number recursively.
package SimplerPrograms; public class NumberofDigits { int Count = 0; public int DigitsCount(int Number) { if(Number > 0) { Count = Count + 1; DigitsCount(Number / 10); } return Count; } }
Within the Main Program to Count Number of Digits in a Number, we will create an instance of the above specified class and call the methods
package SimplerPrograms; import java.util.Scanner; public class NumberofDigitsUsingRecursion { private static Scanner sc; public static void main(String[] args) { int Number, Count=0; sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); Number = sc.nextInt(); NumberofDigits nd = new NumberofDigits(); Count = nd.DigitsCount(Number); System.out.format("\n Number of Digits in a Given Number = %d", Count); } }
NumberOfDigits Class Analysis:
In this Java Program to Count Number of Digits in a Number, we declared an integer function DigitsCount with one argument. Within the function, we used the If statement to check whether the given number is greater than Zero or Not. If it is true, statements inside the If block will execute. It means:
- Count will be incremented by 1 using Count = Count + 1
- DigitsCount (Number / 10) statement calls the function Recursively with the updated value. If you miss this line, after completing the first line compiler terminates.
TIP: If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.
Main Class Analysis:
First, we created an instance or Object of the NumberOfDigits Class
NumberofDigits nd = new NumberofDigits();
Next, calling the DigitsCount method.
Count = nd.DigitsCount(Number);
Lastly, System.out.println statement to print the output (987654321 = 9).