- On
- By
- 0 Comment
- Categories: addition, Java basic
5 methods to add two numbers in Java
5 methods to add two numbers in Java
In this tutorial, we will discuss the concept of 5 methods to add two numbers in Java programming language
In this post, we will learn how to make the addition of two number different 5 methods in Java programming language
Java program to add two numbers
Using the plus(+) Operator: (method 1)
This program allows the user to declare two variables, and calculate sum of two numbers using plus(+) operator and then it displays the results on the screen
Program 1
//Using the plus(+) Operator: public class addition { public static void main (String args[]){ int num1 = 50; int num2 = 30; int sum = num1 + num2; System.out.print("Total is:"+sum); } }
When the above code is executed, it produces the following result
Total is:80
Using Scanner for User Input: (method 2)
This program allows the user to enter two values to variables, and calculate sum of given numbers and then it displays the results on the screen
Program 2
//Using Scanner for User Input: import java.util.Scanner; public class addition { public static void main (String args[]){ Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); // To use the method int result = num1+ num2; System.out.print("Total is:"+result); } }
When the above code is executed, it produces the following result
Enter the first number: 12 Enter the second number: 3 Total is:15
Using a method and User Input: (method 3)
This program allows the user to enter two values to variables and calculate sum of given numbers using Java method, and then it displays the results on the screen.
Program 3
//Using a method and User Input: import java.util.Scanner; public class addition { public static void main (String args[]){//main method Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); // To use the method int sum = add(num1,num2); //call the method to calculate sum System.out.print("Total is:"+sum); } public static int add(int num1, int num2) {//user defined method return num1 + num2; } }
When the above code is executed, it produces the following result
Enter the first number: 120 Enter the second number: 280 Total is:400
Using Command-Line Arguments: (method 4)
Program 4
//Using Command-Line Arguments: public class AddNumbers { public static void main(String[] args) { if (args.length == 2) { int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); int sum = num1 + num2; System.out.println("Sum: " + sum); } else { System.out.println("Please provide two numbers as command-line arguments."); } } }
Using a GUI Application (Swing): (method 5)
Program 5
//Using a GUI Application (Swing): import javax.swing.JOptionPane; public class AddNumbersGUI { public static void main(String[] args) { String input1 = JOptionPane.showInputDialog("Enter the first number:"); String input2 = JOptionPane.showInputDialog("Enter the second number:"); int num1 = Integer.parseInt(input1); int num2 = Integer.parseInt(input2); int sum = num1 + num2; JOptionPane.showMessageDialog(null, "Sum: " + sum); } }
Similar post
C program to add two numbers without using arithmetic operators
C++ program to add two numbers without using arithmetic operators
Python program to add two numbers without using arithmetic operators
Suggested for you
For loop in Java language
While loop in Java language
Operator in Java language
Operator in Java