Write a Java program to compare two numbers

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class Javaexcercise {
 public static void main( String args[] )    
    {
 
        Scanner input = new Scanner(System.in);
        int num1;      
        int num2; 
 
        System.out.print( "Input first integer: " );       
        num1 = input.nextInt();   
 
        System.out.print( "Input second integer: " );       
        num2 = input.nextInt();             
 
        if ( num1 == num2 )           
            System.out.printf( "%d == %d\n", num1, num2 );  
        if ( num1 != num2 )          
            System.out.printf( "%d != %d\n", num1, num2 );  
        if ( num1 < num2 )          
            System.out.printf( "%d < %d\n", num1, num2 );  
        if ( num1 > num2 )          
            System.out.printf( "%d > %d\n", num1, num2 );  
        if ( num1 <= num2 )          
            System.out.printf( "%d <= %d\n", num1, num2 );  
        if ( num1 >= num2 )          
            System.out.printf( "%d >= %d\n", num1, num2 );  
    }
  }

Result

Write a Java program to compare two numbers
Write a Java program to compare two numbers

Leave a Comment