Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Java Program to Count Number of Digits in a Number

by suresh

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);
	}
}
Java Program to Count Number of Digits in a Number using While Loop

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 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;
	}
}
Java Program to Count Number of Digits in a Number using Functions

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);
	}
}
Java Program to Count Number of Digits in a Number using Recursion

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).

Placed Under: Java Programs

  • Java Hello World Program
  • Java Add Two Numbers Program
  • Java Compound Interest Program
  • Java Number Divisible by 5 & 11
  • Java Cube of a Number Program
  • Java Print Even Numbers 1 to N
  • Java GCD of Two Numbers
  • Java LCM of Two Numbers
  • Java Largest of Two Numbers
  • Java Largest of Three Numbers
  • Java Print Multiplication Table
  • Java Odd Numbers from 1 to N
  • Java Odd Even Program
  • Java find +Ve or -Ve number
  • Java Power of a Number Program
  • Java Calculate Profit or Loss
  • Java Print 1 to 100 without Loop
  • Java Quadratic Equation roots
  • Java Square of Number Program
  • Java Simple Interest Program
  • Java Sum of Even Numbers
  • Java Sum of Odd numbers
  • Java Sum of Even & Odd Number
  • Java find Total ,Average & Percentage of 5 Subjects
  • Java ASCII Value of a Character
  • Java ASCII Value of all Characters
  • Java String Comparison program
  • Java Area of Circle Program
  • Java Area Of Triangle Program
  • Java Area Of Trapezoid Program
  • Java Area of Equilateral Triangle
  • Java Area of Rectangle Program
  • Java Area of Right angle triangle
  • Java Volume & Surface of Sphere
  • Java Volume & Surface of Cone
  • Java Volume & Surface of Cuboid
  • Java Volume & Surface of Cube
  • Java Cylinder Volume & Surface
  • Java Math Library
  • Java Armstrong Number Program
  • Java Perfect Number Program
  • Java Palindrome Program
  • Java Count Digits in a Number
  • Java Calculate Electricity Bill
  • Java Find Factors of a Number
  • Java Factorial Program
  • Java Fibonacci Series program
  • Java find First Digit of a Number
  • Java find Last Digit of a Number
  • Java First & Last Digit of Number
  • Java Generic Root of Number
  • Java Natural Numbers in Reverse
  • Java Natural Numbers from 1 to N
  • Java Find Sum of Prime Numbers
  • Java Print Prime Numbers 1 to N
  • Java Prime Number Program
  • Java Reverse a Number Program
  • Java Strong Number Program
  • Java Sum of N Natural Numbers
  • Java Sum of Digits Program
  • Java Swap Two Numbers
  • Java Leap Year Program
  • Java Number of Days in a Month
  • Java Array
  • Java Array Arithmetic Operations
  • Java Program to Copy an Array
  • Java Count Array Duplicates
  • Java count even number in array
  • Java count odd numbers in Array
  • Java Count Even & Odds in Array
  • Java Count Negative Array Items
  • Java Count Positive Array Items
  • Java Count Array +Ve & -Ve num
  • Java Delete Array Duplicates
  • Java Large & Small Array Item
  • Java Largest Array Number
  • Java Print Array Elements
  • Java Print Positive Array Items
  • Java Print Negative Array Items
  • Java Smallest Array Number
  • Java Second Largest Array Item
  • Java Print Unique Array Items
  • Java +Ve & -Ve in separate array
  • Java Sort Array in Descending
  • Java Sort Array in Ascending
  • Java Sum of Array Elements
  • Java Sum of Array Even Numbers
  • Java Sum of Array Odd Numbers
  • Java Sum of Array Even and Odd
  • Java Swap Arrays without temp
  • Java Matrix Introduction
  • Java Print Matrix items
  • Java Check 2 Matrices are Equal
  • Java Matrix Arithmetic operation
  • Java Matrix Addition
  • Java Matrix Multiplication
  • Java Find Matrix Determinant
  • Java Identity Matrix
  • Java Interchange Matrix diagonal
  • Java Matrix Lower Triangle
  • Java Matrix Lower Triangle Sum
  • Java Matrix Upper Triangle
  • Java Matrix Upper Triangle Sum
  • Java Scalar Matrix Multiplication
  • Java Sparse Matrix
  • Java Sum of Matrix Diagonal
  • Java Sum of Matrix Opp Diagonal
  • Java Sum of each Matrix Row
  • Java Sum of each Matrix Column
  • Java Sum of Matrix row & column
  • Java Symmetric Matrix
  • Java Transpose Matrix
  • Java Rectangle Star Pattern
  • Java Square Star Pattern
  • Java Hollow Box Number Pattern
  • Java Box Number Pattern of 1, 0
  • Java Square Number Pattern
  • Java Print Floyd’s Triangle
  • Java Print String Characters
  • Java String replace 1st Char Occ
  • Java String replace last Char occ
  • Java String remove 1st Char Occ
  • Java string remove last Char Occ
  • Java String remove 1st, last char
  • Java String remove all char occur
  • Java String Find 1st Char Occur
  • Java String find all char occur
  • Java String Find Last Char Occur
  • Java String Print 1st & last Char
  • Java String Chars ASCII values
  • Java Program to Concat Strings
  • Java String Characters count
  • Java Count Total Char Occur
  • Java Max Occur String Character
  • Java Toggle String Characters
  • Java Frequency of each Char
  • Java Count Vowels & Consonants
  • Java Count alph digits, special
  • Java Palindrome String
  • Java Program to Reverse a String
  • Java Min Occur String character
  • Java Convert Lower to Upper
  • Java Convert Upper to Lower
  • Java Count total String words

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy