In the previous article, we have seen Java Program to Calculate nPr
In this article we will see how to count total number of digits in a number using java programming language.
Java Program to Count Total Digits in a Number
As we know from 0 to 9 are called as digits and a number is a combination of multiple digits. So our task is to count total number of digits are used in a given number.
Example:
Number= 9034, Total number of digits= 4 Number= 786, Total number of digits= 3 Number= 89, Total number of digits=2
Let’s see different ways to count total number of digits in a number.
Method-1: Java Program to Count Total Digits in a Number By Using Static Input Value
Approach:
- Declare an integer variable ‘
number
‘ and initialize the value. - Declare an integer variable ‘
numberOfDigit
‘ and initialize it to 0. - Inside while loop divide the number by 10 and increment the value of
numberOfDigit
by 1. - Continue the loop till ‘
number
‘ value becomes 0. - Now after the while loop completes the value of ‘
numberOfDigit
‘ represents total number of digits in the given number.
Program:
public class Main { public static void main(String args[]) { //initializing integer variable 'numberOfDigit' to 0 int numberOfDigit = 0; //a number declared int number = 63365; //assigning the value of 'number' to an integer variable 'tmp' int temp=number; //countinue the loop till the number becomes 0 while(number!=0) { //Dividing the number with 10 number = number/10; //incrementing the value of 'numberOfDigit' numberOfDigit++; } System.out.println("Total number of digits in " +temp+ " are "+ numberOfDigit); } }
Output: Total number of digits in 63365 are 5
Method-2: Java Program to Count Total Digits in a Number By Using User Input Value
Approach:
- Declare an integer variable ‘
number
‘ and take the value as user input. - Declare an integer variable ‘
numberOfDigit
‘ and initialize it to 0. - Inside while loop divide the number by 10 and increment the value of
numberOfDigit
by 1. - Continue the loop till ‘
number
‘ value becomes 0. - Now after the while loop completes the value of ‘
numberOfDigit
‘ represents total number of digits in the given number.
Program:
import java.util.Scanner; public class Main { public static void main(String args[]) { //Scanner class object created Scanner sc = new Scanner(System.in); //initializing integer variable 'numberOfDigit' to 0 int numberOfDigit = 0; System.out.println("Enter a number :"); int number = sc.nextInt(); //assigning the value of 'number' to an integer variable 'tmp' int temp=number; //countinue the loop till the number becomes 0 while(number!=0) { //Dividing the number with 10 number = number/10; //incrementing the value of 'numberOfDigit' numberOfDigit++; } System.out.println("Total number of digits in " +temp+ " are "+ numberOfDigit); } }
Output: Enter a number : 893450 Total number of digits in 893450 are 6
Method-3: Java Program to Count Total Digits in a Number By Using User Defined Method
Approach:
- Declare an integer variable ‘
number
‘ and take the value as user input. - Declare an integer variable ‘
numberOfDigit
‘ and initialize it to 0. - Call a user defined method
countDigit()
by passing the number as parameter. - Inside the method take a while loop and inside loop divide the number by 10 and increment the value of
numberOfDigit
by 1. - Continue the loop till ‘
number
‘ value becomes 0. - Now after the while loop completes the value of ‘
numberOfDigit
‘ represents total number of digits in the given number.
Program:
import java.util.Scanner; public class Main { public static void main(String args[]) { //Scanner class object created Scanner sc = new Scanner(System.in); System.out.println("Enter a number :"); int number = sc.nextInt(); //callling a user defined method countDigit() countDigit(number); } public static void countDigit(int number) { //initializing integer variable 'numberOfDigit' to 0 int numberOfDigit = 0; //assigning the value of 'number' to an integer variable 'tmp' int temp=number; //countinue the loop till the number becomes 0 while(number!=0) { //Dividing the number with 10 number = number/10; //incrementing the value of 'numberOfDigit' numberOfDigit++; } System.out.println("Total number of digits in " +temp+ " are "+ numberOfDigit); } }
Output: Enter a number : 657 Total number of digits in 657 are 3
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.
Related Java Programs: