Accept two integers and return true if the either one is 15 or if their sum or difference is 15


This Java program prompts the user to enter two integer numbers, and then determines whether their sum or difference is equal to 15 or if either of the numbers is 15. The program then prints the result to the console.

  • The program first creates a Scanner object called input to read user input from the console. It then prompts the user to enter two integer numbers using the System.out.print statement and reads the user's input using the input.nextInt() method. The entered numbers are assigned to the variables fn and sn.
  • The program then calls the calculate method, passing in fn and sn as arguments. This method takes the two input integers and determines whether their sum or difference is equal to 15, or if either of the numbers is equal to 15. The method uses an if statement to check whether either of the input numbers is equal to 15. If so, it returns true. If not, the method uses a boolean expression to check whether the sum or difference of the two numbers is equal to 15. The method then returns true if the expression is true and false otherwise.
  • Finally, the program prints the result of the calculate method using System.out.println.

Note that the program assumes that the input numbers are integers. If you want to allow for non-integer input or if you want to modify the conditions for determining the result, you would need to modify the calculate method accordingly.

Source Code

import java.util.*;
public class Sum_Difference
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter First Integer Number : ");
		int fn = input.nextInt();  
		System.out.print("Enter Second Integer Number : ");
		int sn = input.nextInt();
		System.out.print("Result is : "+calculate(fn, sn));
	}
 
	public static boolean calculate(int fn, int sn)
	{		
		if(fn == 15 || sn == 15)
			return true;
		return ((fn + sn) == 15 || Math.abs(fn - sn) == 15);
	}
}

Output

Enter First Integer Number : 52
Enter Second Integer Number : 37
Result is : true

Example Programs