Dremendo Tag Line

Check 2 digit or 3 digit positive number in Java

if else if - Question 1

In this question, we will see how to input a number and check if it is a 2 digit or 3 digit positive number or not in Java programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.

Q1) Write a program in Java to input a number and check if it is a 2 digit or 3 digit positive number or not.

Program

import java.util.Scanner;

public class Q1
{
    public static void main(String args[])
    {
        int n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number ");
        n=sc.nextInt();
        if(n>=10 && n<100)
        {
            System.out.print("Its a two digit positive number");
        }
        else if(n>=100 && n<1000)
        {
            System.out.print("Its a three digit positive number");
        }
        else
        {
            System.out.print("Other digit number");
        }
    }
}

Output

Enter a number 184
Its a three digit positive number
video-poster