Compare integers using if statements, relational operators and equality operators


Program

package com.theprogrammersbook.introduction.common;

import java.util.Scanner;

public class MyFirstClass {
	// main method begins execution of Java application
	public static void main(String[] args) {
		// create Scanner to obtain input from command line
		Scanner input = new Scanner(System.in);

		int number1; // first number to compare
		int number2; // second number to compare

		System.out.print("Enter first integer: "); // prompt
		number1 = input.nextInt(); // read first number from user

		System.out.print("Enter second integer: "); // prompt
		number2 = input.nextInt(); // read second number from user

		if (number1 == number2)
			System.out.printf("%d == %d%n", number1, number2);

		if (number1 != number2)
			System.out.printf("%d != %d%n", number1, number2);

		if (number1  %d%n", number1, number2);

		if (number1 = %d%n", number1, number2);
	}
}

Results


Enter first integer: 3
Enter second integer: 6
3 != 6
3 < 6
3 <= 6

Have any Question or Comment?

Leave a Reply

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