Java program to check if the number is greater than 100 or not

In this section, we will learn two java programs, first java programs checks whether the specified number is greater than 100 or not. The second program takes the input number (Entered by user) and checks whether it is greater than 100 or not and displays the result.

How the program will work?

  • If a number is greater than zero and less than 100 then it is “less than 100”.
  • If a number is greater than 100 then it is “greater than 100”.
  • If a number is less than zero then it is “Invalid Input”.

Lets write this logic in a Java Program.

Java program to check if the number is greater than 100 or not
Flow chart number is greater than 100 or not

Example:

num=106
if num>100
Number is greater than 100
else
Number is less than 100

Example 1: Program to check whether the given number is greater than 100

In this program, we have specified the value of the number during initializing and the program checks whether the specified number is greater than 100 or not. To understand this program you should have the basic knowledge of the if-else statement in Core java.

public class number_greater_than_100 {
    public static void main(String[] args) {

        int number=106;
        if(number>0){

            if (number>100){
                System.out.println("Number is greater than 100");
            }
            else {
                System.out.println("Number is less than 100");
            }

        }
        else{
            System.out.println("Invalid Input");
        }

    }
}

Output:

Number is greater than 100

Example 2: Check whether the input number (entered by the user) is greater than 100

Here we are using a Scanner to read the number entered by the user and then the program checks and displays the result. To understand this program you should have the basic knowledge of the Scanner.

import java.util.Scanner;

public class number_greater_than_100 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number:");
        int number=sc.nextInt();
        if(number>0){

            if (number>100){
                System.out.println("Number is greater than 100");
            }
            else {
                System.out.println("Number is less than 100");
            }

        }
        else{
            System.out.println("Invalid Input");
        }

    }
}

Output:

Enter the number:
10
Number is less than 100

Recommended

  1. Java Program to Add Two Integers
  2. Java program to convert Hexadecimal to octal number?
  3. How do you reverse a number without a loop in Java?

Leave a Comment

Your email address will not be published. Required fields are marked *

x