Beta Coder

Learn code like Beta Coder.

Breaking

Monday, September 21, 2020

Java Program to Count No. of Digits of a Number

COUNT NO. OF DIGITS OF A NUMBER

JAVA COUNT NO. OF DIGITS OF A NUMBER
In this program we learn how to count no. of digits of a number by provided user using Java.
Java program to count no. of digits of a number
// COUNT NO. OF DIGITS OF A NUMBER
import java.util.*;
class CountDigits
{
	int n;
	public
		CountDigits()
{ Scanner in = new Scanner(System.in); System.out.println("Enter a number : "); n = in.nextInt(); } int count() { int s = 0; do { n = n / 10; s++; }while(n != 0); return(s); } void display(int p) { System.out.println("The no. of digits : "+p); } public static void main(String arg[]) { CountDigits c = new CountDigits();
int p; p = c.count(); c.display(p); } };
OUTPUT:
   Enter a number : 12542
   The no. of digits : 5

No comments:

Post a Comment