WAP to input a positive number and check whether it a single, double, triple or ‘n’ digit number.
Answers
Answered by
1
import java.util.Scanner;
public class NumberDigits {
public static void main(String[ ] args) {
// Accepting a number and converting it to String.
System.out.print("Enter a number - ");
// Using nextLong( ) to ensure that the user enters a Integer.
String number = new Scanner(System.in).nextLong( ) + "";
System.out.println("It's a " + number.length() + " digit number");
}
}
Can be further modified with a switch statement,
switch (number.length( )) {
case 1:
System.out.println("Single digit number");
case 2:
System.out.println("Double digit number");
case 3:
System.out.println("Triple digit number");
....
}
Similar questions