Java Program to Add Two Numbers

Write a Java Program to Add Two Numbers and print the output. This simple Java program lets the user enter two integer values and then add those two numbers and assign it to a variable sum.

package SimpleNumberPrograms;

import java.util.Scanner;

public class addTwoNumbers {

 private static Scanner sc;

 public static void main(String[] args) {
 int Number1, Number2, Sum;
 sc = new Scanner(System.in);
 
 System.out.println("\n Please Enter the First integer Value: ");
 Number1 = sc.nextInt();

 System.out.println("\n Please Enter the Second integer Value: ");
 Number2 = sc.nextInt();
 
 Sum = Number1 + Number2;
 System.out.println("\n Sum of the two integer values is = " + Sum);
 }
}

Java Add Two Numbers output

 Please Enter the First integer Value: 
10

 Please Enter the Second integer Value: 
25

 Sum of the two integer values is = 35

Within this Java Program to Add Two Numbers, we used Arithmetic Operators + to add Number1 and Number2 and then assigned that total to Sum.

Sum = Number1 + Number2;

The following Java System.out.println statement will print the sum variable as output (10 + 25 = 35).

System.out.println("\n Sum of the two integer values is = " + Sum);

Simple Java Program to Add Two Numbers using Methods

In this Java program, we are using the same steps that we followed in our first example. Still, we separated the Java Program to Add Two Numbers logic and placed it in a separate method.

//Java Program to Add Two Numbers using Methods
package SimpleNumberPrograms;

import java.util.Scanner;

public class addTwoNumbersMethod {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int Number1, Number2, Sum;
		sc = new Scanner(System.in);
	
		System.out.println("\n Please Enter the First integer Value: ");
		Number1 = sc.nextInt();
		//System.out.println("\n Please Enter the Second integer Value: ");
		Number2 = sc.nextInt();
		
		Sum = add(Number1, Number2);
		System.out.println("\n Sum of the two integer values is = " + Sum);
	}
	
	public static int add(int Number1,int Number2){
		int Sum;
		Sum = Number1 + Number2;
		return Sum;
	}
}

Java Add Two Numbers using functions output

 Please Enter the First integer Value: 
45
95

 Sum of the two integer values is = 140

If you observe the following statement, we are calling the add method and assigning the return value to integer variable Sum

Sum = add(Number1, Number2);

Let us see the code inside the add method

public static int add(int Number1,int Number2){
		int Sum;
		Sum = Number1 + Number2;
		return Sum;
	}

From the above code snippet, you can observe that this method accepts two arguments of integer type.

  • Within the function, we declared an integer variable Sum to store the added value.
  • Next, we performed the arithmetic operation to add two integer numbers.
  • Lastly, we are returning Sum

Java Program to Add Two Numbers using OOPs

In this example, we are dividing the code further using the Object Oriented Programming. To do this, First, we will create a class that holds two methods.

TIP: In general, You don’t have to write the first method. We used this to show the available options

package SimpleNumberPrograms;

public class AddNumbers {
	int x, y, Sum;
	
	public void add(){
		Sum = x + y;
		System.out.println("\n From Class: Sum of two integer values is = " + Sum);
	}
	
	public int addAgain(int Number1,int Number2){
		Sum = Number1 + Number2;
		return Sum;
	}
}

Within the Main Java Program to Add Two Numbers, we will create an instance of the above specified class and call the methods

// Calling AddNumbers Class
package SimpleNumberPrograms;

import java.util.Scanner;

public class addTwoNumbersClass {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number1, Number2, Sum;
		sc = new Scanner(System.in);
	
		System.out.println("\n Please Enter the First integer Value: ");
		Number1 = sc.nextInt();
		System.out.println("\n Please Enter the Second integer Value: ");
		Number2 = sc.nextInt();
		
		AddNumbers addNum = new AddNumbers();
		addNum.x = Number1;
		addNum.y = Number2;
		
		addNum.add();
		Sum = addNum.addAgain(Number1, Number2);
		System.out.println("\n Sum of the two integer values is = " + Sum);
	}

}
Java Program to Add Two Numbers 3

AddNumbers Class in Add Two Numbers Analysis:

  • First, we declared a function add with zero arguments. Within the function, we performed the arithmetic addition and printing the Output using System.out.println statement.
  • Next, we declared an integer function addAgain with two arguments. Within the function, we performed the arithmetic addition and returning the integer value.

Main Class Analysis:

In the Java Program to Add Two Numbers main class, First, we created an instance of the AddNumbers Class

AddNumbers addNum = new AddNumbers();

Next, we are assigning the user entered values to the AddNumbers Class variables.

addNum.x = Number1;
addNum.y = Number2;

Next, we are calling the add method. Note, this method will add two numbers and print the output from the AddNumbers Class itself.

addNum.add();

Next, we are calling the addAgain method. Note, this is the second method with an integer data type.

Sum = addNum.addAgain(Number1, Number2);