Q:

Java program to compare two numbers with each other

belongs to collection: Java Basic Programs

0

Given two integer numbers and we have to compare them with each other using java program.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to compare two numbers with each other in java

import java.util.Scanner;

public class CompareTwoNumbers
{
	public static void main( String args[] )    
    {
        // create objects       
        Scanner sc = new Scanner(System.in);
        int number1;        
        int number2;   
      
        // enter both the numbers for comparison.
        System.out.print("Enter first number : " );        
        number1 = sc.nextInt();    
 
        System.out.print("Enter second number : " );         
        number2 = sc.nextInt();               
        
        if ( number1 == number2 )           
            System.out.printf( "%d == %d\n", number1, number2 );  
        if ( number1 != number2 )          
            System.out.printf( "%d != %d\n", number1, number2 );  
        if ( number1 < number2 )          
            System.out.printf( "%d < %d\n", number1, number2 );  
        if ( number1 > number2 )          
            System.out.printf( "%d > %d\n", number1, number2 );  
        if ( number1 <= number2 )          
            System.out.printf( "%d <= %d\n", number1, number2 );  
        if ( number1 >= number2 )          
            System.out.printf( "%d >= %d\n", number1, number2 );  
    }
}

Output

First run:
Enter first number : 12
Enter second number : 10
12 != 10
12 > 10
12 >= 10

Second run:
Enter first number : 12
Enter second number : 10
12 != 20
12 < 20
12 <= 20

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to calculate area of Hexagon... >>
<< Program to verify answers of answer sheets of N st...