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

Java Code: 5 Ways to Add Two Numbers (With Examples)

Posted on September 30, 2025October 2, 2025

Table of Contents

  • Java Code: 5 Ways to Add Two Numbers (With Examples)
    • 5 Ways to Add Two Numbers (With Examples)
      • Using simple arithmetic operator
      • Using a method
      • Using constructure
      • Using User input
      • Using Recursion
    • Related

Java Code: 5 Ways to Add Two Numbers (With 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

Java Code: 5 Ways to Add Two Numbers (With Examples)
Add Two Numbers

5 Ways to Add Two Numbers (With Examples)

Using simple arithmetic operator

In this program, we use a simple arithmetic operator to add two numbers.

Program 1

/*Calculate sum of two numbers in java using + operator */
 //Using the plus(+) Operator:
 public class addition {
      public static void main (String args[]){
  
   int num1 = 50;
   //variable declaration and initialization of num 1
   int num2 = 30;
    //variable declaration and initialization of num 2
   int sum = num1 + num2;
   //Calculate sum of two numbers in java using + operator
   System.out.print("Total is:"+sum);
   //Print the result on the screen
   
 
  }
  }

When the above code is executed, it produces the following result

Total is : 80

 

Using a method

In this program, we use a User defined method to add two numbers.

Program 2

//addition of two numbers using a method
//in java programming

class AddTwoNum{
    public static void main(String[] args) {
        int num1=56;
        int num2=23;
        int sum=addNum(num1,num2);
        System.out.println("Sum: "+sum);
    }
    public static int addNum(int a, int b){
     return a+b;   
    }
}

When the above code is executed, it produces the following result

Sum: 79

 

Using constructure

In this program, we use a constructor to add two numbers.

Program 3

//Addition two numbers using constructor
//in Java Programming 
public class Cons_Add{
    private int result;
    public Cons_Add(int a, int b){
        this.result=a+b;
    }
    
    public int get_Result(){//user defined method
        return result;
    }
    public static void main(String[] args) {
int num_1=75;
int num_2=55;
    
        Cons_Add add=new Cons_Add(num_1,num_2);
        System.out.println(" result :"+add.get_Result());
    }
}

When the above code is executed, it produces the following result

result :130

 

Using User input

In this program, we use user-input to add two numbers.

Program 4

//Addition two numbers using user inputr
//using simple additional operator in Java Programming 
import java.util.Scanner;
public class NormalAdd{

    
    public static void main(String[] args) {
      Scanner input=new Scanner(System.in);
 System.out.println("Input value for num1:");
int num_1=input.nextInt();

 System.out.println("Input value for num2:");
int num_2=input.nextInt();
    int result=num_1+num_2;
             System.out.println("Addition of two numbers using  constructor:");
        System.out.println(" result :"+result);
    }
}

When the above code is executed, it produces the following result

Input value for num1:
35
Input value for num2:
45
Addition of two numbers using constructor:
result :80

 

Using Recursion

In this program, we use a recursive method to add two numbers.

Program 5

//addion java two num Using recursion
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) {
        int num1=34;
        int num2=66;
        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

Sum of 34 and 66 : 100

 

Now you can see 5 different Java programs to add two numbers

 

Similar post

 Java program to Add two numbers 5 different methods

PHP: Addition of two numbers

C language : Add two numbers  5 different methods

C+ language : Add two numbers  5 different methods

 

 

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