Computer Science, asked by UtkarshChawhan, 5 months ago


Write a Java program to input an integer from the user. Check and display if the number
input is a two digit number, a three- digit number or a four digit number, eise display
number not in range".
Input: 200
Output: It is a three-digit number​

Answers

Answered by anindyaadhikari13
5

Required Answer:-

Question:

  • Write a Java program to input an integer from the user. Check and display if the number entered is a two digit number, a three digit number Or a four digit number. Else display "Number not in range."

Solution:

This is an easy question. Here is the code.

import java.util.*;

public class NumberOp {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n=sc.nextInt();

if (n>9&&n<100)

System.out.println("Number is a two digit number.");

else if (n>99&n<1000)

System.out.println("Number is a three digit number.");

else if (n>999&&n<10000)

System.out.println("Number is a four digit number.");

else

System.out.println("Number not in range.");

sc.close();

}

}

At first, we will take the number as input from the user.

  • If the number is greater than 9 and less than 100 then it's a two digit number.
  • If the number is greater than 99 and less than 1000 then the number is a three digit number.
  • If the number is greater than 999 and less than 10000 then it's a four digit number.

Using if else, we will check these condition and as per the condition, we will display the message whether it's a two digit, a three digit or a four digit number or not.

Output is attached.

Attachments:
Answered by BrainlyProgrammer
2

Question:-

  • WAP in JAVA to check if entered number is two digit three digit or four digit
  • Else display "Number not in range".

Sample Input:-

200

Sample Output:-

It is a three-digit number

Code:-

package Coder;

import java.util.*;

public class Number

{

public static void main(String[] args)

{

Scanner sc=new Scanner (System.in);

System.out.println("Enter a Number");

int n=sc.nextInt();

String str=(n>9&&n<100)?"two-digit number":(n>=100&&n<1000)?"three-digit number":(n>=1000&&n<=9999)?"four-digit number":"not in range";

System.out.println("The number is "+str);

}

}

#Shortest code possible

______

Variable Description:-

  • n:- to accept number as input from the user
  • str:- to store the string according to the condition given

___

How will this code work?

  • The program check the condition according to the entered number
  • if entered number is >9 and <100 Then str stores "two digit number"
  • same in other conditions
  • If entered number is >9999 then str will store "not in range".
  • Finally, we prints the Output

___

•Output attached

Attachments:
Similar questions