Java: Accepts three integers and check whether sum of the first two given integers is greater than third one
Check if Sum of Two Integers is Greater than Third
Write a Java program that accepts three integers and checks whether the sum of the first two integers is greater than the third integer. Three integers are in the interval [-231, 231 ].
Visual Presentation:
Sample Solution:
Java Code:
// Importing Scanner class for user input
import java.util.Scanner;
// Defining a class named "Main"
public class Main {
// Main method, the entry point of the program
public static void main(String[] args) {
// Creating a Scanner object for reading user input
Scanner in = new Scanner(System.in);
// Prompting the user to input three integers (a, b, c)
System.out.println("Input three integers (a, b, c):");
// Reading long integers a, b, and c from the user
long a = in.nextLong();
long b = in.nextLong();
long c = in.nextLong();
// Prompting the user with a message
System.out.println("Check whether (a + b) is greater than c?");
// Checking and printing whether the sum of a and b is greater than c
System.out.println(a + b > c);
}
}
Sample Output:
Input three integers (a,b,c): 5 8 9 Check whether (a + b) is greater than c? true
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to check if the sum of any two out of three integers is greater than the third for all combinations.
- Write a Java program to verify if the sum of two integers is greater than the third while also ensuring their product is below a certain threshold.
- Write a Java program to check if the sum of the first two integers exceeds the third and if their difference is a prime number.
- Write a Java program to determine if the sum of two integers is greater than the third when the integers are provided in random order.
Go to:
PREV : Convert 3-Digit Numbers to "HHTTT123" Format.
NEXT : Check if Word is Abecadrian.
Java Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.