Find Number of Digits in Given Number in Java

Prev Tutorial Next Tutorial

Find Number of digits in Given Number Program in Java

Any number is a combination of digits, like 342 have three digits. For calculating the number of digits of any number user have three possibilities to input values.

  • Enter +ve Number
  • Enter -ve Number
  • Enter zero

Find Number of digits in Given Number in Java

import java.util.Scanner;
class Numofdigit 
public static void main(String[] args) 
 int no,a=0;
 Scanner s=new Scanner(;
System.out.println("Enter any number : ");
 no = s.nextInt();

no=no * -1;
else if(no==0)
System.out.println("Number of Digits in given number is: "+a);


Enter any number:
Number of Digits in given number is: 3

Syntax to compile and run java program


for compile -> c:/javac
for run -> c:/java Numofdigit

Explnation of Code

  • Scanner s=new Scanner( are used for receive input from keyboard.
  • nextInt(): method are used for get integer type value from keyboard.
  • System.out.println("....."): are used for display message on screen or console.

Explanation of program


Here we find remainder of given number, using Modulo Operator we find remainder of any number, Using this step we get only last digits.


Here After dividing any number by 10 we get all digits except last digits.


Using this statement we find sum of all digits. In first iteration it add 0 and last digits and store in Sum variable, in second iteration it add Previous sum values and last digits of new number and again store in Sum variable, and so on upto while condition is not false.

Prev Tutorial Next Tutorial

Yahoo Advertisements

Buy 3 Year Old Domain,, @$200

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on:

Download Projects

Adsense Advertisements
Free Classified Site – Bedpage

Yahoo Ads Advertisements