How can we write program in java to find biggest number among two numbers? | Core Java Forum
H
Harika Posted on 05/07/2023

can you explain me clearly how can we write the program to find biggest number among two numbers using if else statement but I should give values to the variables at the time of compilation only that means when i'm trying to run the program it should ask me to enter two values and it should give result please tell me how to write that program


Y
Yogesh Chawla Replied on 06/07/2023

Please see this:

package com.programming.class18;

import java.util.Scanner;

public class BiggestOfTwo {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in); //Using Scanner, we can take the input at runtime
		System.out.println("Enter First Number--- ");
		int firstNumber = scanner.nextInt();
		System.out.println("Enter Second Number--- ");
		int secondNumber = scanner.nextInt();
		int difference = firstNumber - secondNumber;
		System.out.println("Difference is " + difference);

		//This is how comparison goes
		if (firstNumber == secondNumber)
			System.out.println("both are equal");
		else if (firstNumber > secondNumber)
			System.out.println("First Number " + firstNumber + " is greater");
		else
			System.out.println("Second Number " + secondNumber + " is greater");

	}
}