Have you just started learning Java? This is the best page you landed, it talks about Adding Two Numbers in Java, and this is the basic programming that a person should know about.
Adding Two Numbers in Java
Adding Two Numbers in Java talks about how to add two numbers in Java, by taking input from Users. Here it will show the 4 different ways for Adding Two Numbers in Java. But before, we land towards the code, we should know about the basic Algorithm. Let’s see that first.
Algorithm for Adding Two Numbers in Java
1. Start
2. Import the Scanner class to read user input.
3. Create a Scanner object named scanner to read user input.
4. Prompt the user to enter the first number.
5. Read the user’s input as a double and store it in the variable num1.
6. Prompt the user to enter the second number.
7. Read the user’s input as a double and store it in the variable num2.
8. Calculate the sum of num1 and num2 and store it in the variable sum.
9. Display the result to the user, showing the sum of num1 and num2.
10. Close the Scanner object to release system resources.
11. End
Example 1: Program for Adding Two Numbers in Java
import java.util.Scanner; // Import the Scanner class to read user input public class Main { public static void main(String[] args) { // Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); // Prompt the user to enter the first number System.out.print("Enter the first number: "); double num1 = scanner.nextDouble(); // Read the first number from the user // Prompt the user to enter the second number System.out.print("Enter the second number: "); double num2 = scanner.nextDouble(); // Read the second number from the user // Calculate the sum of the two numbers double sum = num1 + num2; // Display the result to the user System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum); // Close the Scanner to release system resources scanner.close(); } }
The Output of the above Program is like this:
Also learn, Prime Number Program in Java
Example 2: Progam for Adding Two Numbers in Java using BufferedReader class
//This alternative method is to implement the same code using a different approach. In this alternative version, we'll use the BufferedReader class to read user input: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { // Create a BufferedReader object to read user input BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { // Prompt the user to enter the first number System.out.print("Enter the first number: "); double num1 = Double.parseDouble(reader.readLine()); // Read the first number from the user // Prompt the user to enter the second number System.out.print("Enter the second number: "); double num2 = Double.parseDouble(reader.readLine()); // Read the second number from the user // Calculate the sum of the two numbers double sum = num1 + num2; // Display the result to the user System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum); } catch (IOException e) { // Handle any IOException that may occur while reading input System.err.println("An error occurred while reading user input: " + e.getMessage()); } catch (NumberFormatException e) { // Handle NumberFormatException if the input is not a valid number System.err.println("Invalid input. Please enter valid numbers."); } finally { try { // Close the BufferedReader to release system resources reader.close(); } catch (IOException e) { // Handle any IOException that may occur while closing the reader System.err.println("An error occurred while closing the reader: " + e.getMessage()); } } } }
The Output of the above Program is like this:
Also learn, Java Interview Questions and Answers
Example 3: Progam for Adding Two Numbers in Java Demonstrating Graphical Interface
import javax.swing.JOptionPane; public class AdditionProgram { public static void main(String[] args) { // Prompt the user to enter the first number using a dialog box String input1 = JOptionPane.showInputDialog("Enter the first number:"); double num1 = Double.parseDouble(input1); // Prompt the user to enter the second number using a dialog box String input2 = JOptionPane.showInputDialog("Enter the second number:"); double num2 = Double.parseDouble(input2); // Calculate the sum of the two numbers double sum = num1 + num2; // Display the result in a dialog box JOptionPane.showMessageDialog(null, "The sum of " + num1 + " and " + num2 + " is: " + sum); } }
The Output of the above Program is in 3 steps:
Step 1:
Step 2:
Step 3:
Conclusion
In this article, we have discussed the program for Adding Two Numbers in Java. We have covered multiple ways, starting from the very easy one, to the graphical interface also, that is user can make a block type of structure for taking input and showing Output.