Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

5 Ways to Add Two Numbers in Java with User Input (Examples)

Posted on October 15, 2025October 15, 2025

Table of Contents

  • 5 Ways to Add Two Numbers in Java with User Input (Examples)
    • Add Two Numbers (With Examples)
      • Using ‘+’ operator with user input
      • Using a method with user input
      • Using a recursive method with user input
      • Using a constructor with user input
      • Using a for Loop
    • Related

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

5 Ways to Add Two Numbers in Java with User Input (Examples)
Addition of two numbers

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 

 

10 ways to add two numbers in pascal

Java program to subtract two numbers using loop

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes