In java, to count the digits of a given number we can use loop concepts such as while loop or for loop. We are going to count the digits(Counting the how many times a number is divided by 10). In this article, we are focusing on learning through examples, algorithm, logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We have to write a java program to count the digits of a given integer number. Our program should take an integer number as input and should return the count of total digits present in the input integer/number.
Let’s see an example,
Example:
For input integer num=123.
Output should be 3 as 123 has total 3 digits.
Algorithm to count the digits of a given number
Start:
- Input: Take an integer value
num
.
Initialize:
- Set
count
to 0.
Condition Check:
- While
num
is not equal to 0:- Increment
count
by 1:count++
. - Divide
num
by 10:num = num / 10
.- This removes the last digit of
num
.
- This removes the last digit of
- Increment
End of Loop:
- When
num
becomes 0, exit the loop. - Print the value of
count
.
Terminate:
- The program ends.
Logic to Count the digits of a given number
There are multiple logics to count the digits of a given number in java programming language.
Logic 1: Using while loop
- After taking input value in num variable, and initialize count as zero, we should keep the condition that num should not equal to zero, then inside the while loop, we should divide the num by 10 and then increase the count by 1.
- We should continue the iteration until we get num as zero and count should be incremented by 1 for every iteration. Then we should come out of the loop and print the count.
- Here we are dividing the number by 10 to reduce the number coz we have to count the number.
int count=0;
while(num!=0) {
num/=10;
count++;
}
System.out.println("Number of digits in a given number: "+count);
Logic 2: Using for loop
- After taking an input value in num variable, and initialize count as zero, we should assign num value to another variable i and then keep the condition i!=0, and inside the for loop, we should divide i by 10 and increment the count by 1.
- After completing the iterations, it will come out of loop and print the count.
- Here count variable will contain the number of digits in the input number.
int count=0;
for (int i=num;i!=0;) {
i/=10;
count++;
}
System.out.println("Number of digits in a given number: "+count);
Java program to count digits of a given number
Program 1: Using while loop
In the below program we are using while loop to count digits of a given number.
import java.util.Scanner;
public class CountDigits {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int num=s.nextInt();
int count=0;
while(num!=0) {
num/=10;
count++;
}
System.out.println("Number of digits in a given number: "+count);
s.close();
}
}
Output:
Enter a number
24356
Number of digits in a given number: 5
Java program explanation to count the digits of a given number
- The program imports the Scanner class to take input from the user.
- It defines a class named CountDigits.
- In the main method:
- It creates a Scanner object named ‘s’ to read input from the user.
- Asks the user to enter a number.
- Reads an integer input from the user and stores it in the variable ‘num’.
- Initializes a variable named ‘count’ to keep track of the number of digits, setting it to 0 initially.
- Enters a while loop that continues as long as ‘num’ is not equal to 0.
- Inside the loop, it divides ‘num’ by 10 in each iteration (essentially removing the last digit) and increments ‘count’ by 1.
- Once ‘num’ becomes 0, the loop stops.
- Prints the count of digits in the given number.
- Closes the Scanner object to release system resources.
Program 2: Using for loop
In the below program we are using for loop to count digits of a given number.
import java.util.Scanner;
public class CountDigits {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int num=s.nextInt();
int count=0;
for (int i=num;i!=0;) {
i/=10;
count++;
}
System.out.println("Number of digits in a given number: "+count);
s.close();
}
}
Output:
Enter a number
0034567
Number of digits in a given number: 5
Java program explanation to count the digits of a given number
- This Java program counts the number of digits in a given integer input by the user.
- It imports the Scanner class to facilitate user input.
- Defines a class named CountDigits.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a number.
- Reads the integer input and stores it in ‘num’.
- Initializes a variable ‘count’ to keep track of the number of digits, initially set to 0.
- Enters a for loop that iterates as long as ‘i’ (initialized with ‘num’) is not equal to 0:
- In each iteration, ‘i’ is divided by 10 (essentially removing the last digit) and ‘count’ is incremented by 1.
- Once ‘i’ becomes 0, the loop stops.
- Prints the count of digits in the given number.
- Closes the Scanner object to release system resources.
Conclusion
In this tutorial we have learnt writing the java program to count the digits in a given number using while loop and for loop. Hope this tutorial helped you in understanding this java program.
You can also check our programs
- Java Program to check a given number is Perfect or not.
- Java Program to check all Perfect numbers in a given range.
- Java Program to check a given number is Strong number or not.
- Java Program to print all Strong numbers in a given range.
- Program in Java to swap first and last digit of number.
- Java Program to swap values using third variable.
Happy Coding !!