4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has.
Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input
2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and so on
3) After using if else statement for any possibility in the end just declare that number is not between 1 and 99999
4) Output condition should look something like this:
OUTPUT EXAMPLE:
Enter a number:
56891
It’s a five digit number
Have a look at the below code. Its quite intutive to understand.
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int number = sc.nextInt();
if (number>=1 && number<=9){
System.out.println("It’s a one digit number");
}
else if(number>=10 && number<=99){
System.out.println("It’s a two digit number");
}
else if(number>=100 && number<=999){
System.out.println("It’s a three digit number");
}
else if(number>=1000 && number<=9999){
System.out.println("It’s a four digit number");
}
else if(number>=10000 && number<=99999){
System.out.println("It’s a five digit number");
}
// Like this you can ad as many conditions as per your requirement...
}
}
Happy Learning!
Get Answers For Free
Most questions answered within 1 hours.