Table of Contents
5 Ways to Add Two Numbers in Java with User Input (Examples)
In this article, we will discuss the concept of Java Code: 5 Ways to Add Two Numbers (With Examples)
In this post, we are going to learn how to write a program to 5 Ways to Add Two Numbers (With Examples) in Java programming language.
here, I’ll give you separate short programs for each method so it’s easier to understand

Add Two Numbers (With Examples)
Using ‘+’ operator with user input
In this post, we will use simple arithmetic operator to find sum of two numbers
Program 1
//Addition two numbers using user input
//using simple addition operator in Java Programming
import java.util.Scanner;
public class Simple_Addition{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter value for num1:");
int num1=input.nextInt();
System.out.println("Enter value for num2:");
int num2=input.nextInt();
int result=num1+num2;
System.out.println(" result :"+result);
}
}
When the above code is executed, it produces the following result
Enter value for num1:
25
Enter value for num2:
45
result :70
Using a method with user input
In this post, we will use a custom method with user input to find sum of two numbers
Program 2
//addition of two numbers using a method
//in java programming using user input
import java.util.Scanner;
class AddTwoNum{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Input value for num1:");
int num1=input.nextInt();
//reading the first value
System.out.println("Input value for num2:");
int num2=input.nextInt();
//reading the second value
int sum=addNum(num1,num2);
//calling the method for output
System.out.println("Sum: "+sum);
}
public static int addNum(int a, int b){
return a+b;
//user defined method
}
}
When the above code is executed, it produces the following result
Input value for num1:
46
Input value for num2:
44
Sum: 90
Using a recursive method with user input
In this post, we will use a recursive method with user input to find sum of two numbers
Program 3
//addion java two num Using recursion
import java.util.Scanner;
class AddTwoNumUsingRecursion {
//recursive function add two numbers
public static int addTwoNum(int a,int b){
if(b==0){
return a;//Base case:If b become zero, result is a
}
return addTwoNum(a+1, b-1);//increment a, dicrement b
}
public static void main(String[] args) {
Scanner input=new Scanner (System.in);
System.out.println("Enter first number: ");
int num1=input.nextInt();
System.out.println("Enter second number: ");
int num2=input.nextInt();
int sum=addTwoNum(num1,num2);
System.out.println("Sum of " + num1 + " and "+ num2+" : "+ sum);
}
}
When the above code is executed, it produces the following result
Enter first number:
45
Enter second number:
55
Sum of 45 and 55 : 100
Using a constructor with user input
In this post, we will use a constructor with user input to find sum of two numbers
Program 4
//Addition two numbers using constructor
//in Java Programming
import java.util.Scanner;
public class Cons_Add{
private int result;
//global variable
public Cons_Add(int a, int b){
this.result=a+b;
}//using this keyword
public int get_Result(){//user defined method
return result;
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Input value for num1:");
int num1=input.nextInt();
System.out.println("Input value for num2:");
int num2=input.nextInt();
System.out.println("Addition of two numbers using constructor:");
Cons_Add add=new Cons_Add(num1,num2);
System.out.println(" result :"+add.get_Result());
}
}
When the above code is executed, it produces the following result
Input value for num1:
56
Input value for num2:
67
Subtraction of two numbers using constructor:
result :123
Using a for Loop
Program 5
//add two numbers in java using loop (Increment method)
import java.util.Scanner;
class AddTwoNumUsingLoop {
public static void main(String[] args) {
Scanner input=new Scanner (System.in);
System.out.println("Enter first number: ");
int num1=input.nextInt();
System.out.println("Enter second number: ");
int num2=input.nextInt();
int sum=num1;
for(int i=0; i<num2; i++){
sum++;
}
System.out.println("Sum = : "+sum);
}
}
When the above code is executed, it produces the following result
Enter first number:
125
Enter second number:
225
Sum = : 350
Similar post
5 Ways to Add Two Numbers in Java(Examples)
Add Two Numbers in JavaScript in 4 different ways
Multiply Two Numbers in JavaScript in 4 different ways
Divide Two Numbers in JavaScript in 4 different ways