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 program to addition of two numbers | 5 different Methods

Posted on October 10, 2019October 12, 2019

Table of Contents

  • .Java program to addition of two numbers
    • Program 1: Sum of two numbers -standard method
    • Program 2: Sum of two numbers(entered by user);
    • Sum of two numbers – User defined method
    • Sum of two numbers- User recursion
    • Sum of two numbers – without Arithmetic operator

.Java program to addition of two numbers

In this tutorial, we will discuss the  Java program to addition of two numbers 

Java program to addition of two numbers
Addition of two numbers

In this post we are going to learn how to find sum of two numbers through different 5 ways

Method 1

Program 1: Sum of two numbers -standard method

public class AddTwoNumber{
public static void main(String args[]){
int num1=10,num2=15,sum;//Declare variables num1,num2,sum;
sum=num1+num2;  //find sum of two numbers
System.out.println("Sum of two numbers: "+sum);
//display the sum of the numbers
}
}

When the above code is executed it produces the following output

Sum of two numbers: 25

In this program

  1. Integer variable num1,num2 are declared and initialized
  2. the num1 and num 2 both are added together  and the value is added to the sum
  3. Then, the program is displayed the output  of sum (Sum of two integers) using System.out.println()

Method 2

Program 2: Sum of two numbers(entered by user);

import java.util.Scanner;
public class AddTwoNumbers1{
public static void main(String args[]){
int num1,num2,sum;

Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter Two numbers for addition: ");
num1=scan.nextInt();
num2=scan.nextInt();
//reads the numbers from the user
//input from keyword

sum=num1+num2;  //find sum of two numbers
System.out.println("Sum of two numbers: "+sum);
//display the sum of the numbers

}
}

When the above code is executed it produces the following output

Enter two numbers for addition: 345
570
Sum of two numbers: 915

Approach

  1. Declare variables num1,num2,sum
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2(variables)
  4. The program will read the input using Scanner class and stores in the variables num1 and num2 respectively
  5. the num1 and num 2 both are added together  and the value is added to the sum
  6. Then, the program displays the value of the sum using System.out.print() function

 

Method 3

Sum of two numbers – User defined method

import java.util.Scanner;
public class AddTwoNumbers2{
public static void main(String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter Two numbers for addition: ");
int num1=scan.nextInt();
int num2=scan.nextInt();
//reads the numbers from the user
//input from keyword


System.out.println("Sum of two numbers: "+addition(num1,num2));
//display the sum of the numbers
}

static int addition(int x, int y){ //Define a user defined method
return x+y;


}
}

When the above code is executed it produces the following output

Enter two numbers for addition: 125
250
Sum of two numbers: 375

Approach

  1. Declare variables num1,num2,sum
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2
  4. The program will read the input using Scanner class and store in the variables num1 and num2 respectively
  5. Define the method (addition()) for find sum
  6. Call the method to  produced output
  7. Then, the program Displays the value of sum using System.out.println() function

 

Method 4

Sum of two numbers- User recursion

import java.util.Scanner;
class SumOfNum{
public static void main(String args[]){
    int sum;  //variable declaration
    Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the value for num1: ");
int num1=scan.nextInt();
System.out.print("Enter the value for num2: ");
int num2=scan.nextInt();

     sum=add(num1,num2);
   System.out.print("Sum of two numbers are:"+sum);
} 
static int add(int x, int y)
{
    if(y==0)
        return x;
    else
        return(1+add(x,y-1));
}
}

When the above code is executed it produces the following output

Enter the value for num1: 26
Enter the value for num2: 54
Sum of two numbers are: 80

Approach

  1. Declare variables num1,num2,sum
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2
  4. The program will read the input using Scanner class and store in the variables num1 and num2 respectively
  5. Define the method (addition()) for find sum recursively
  6. Call the method to produced output
  7. Then, the program Displays the value of sum using System.out.printf() function

Method 5

Sum of two numbers – without Arithmetic operator

import java.util.Scanner;
class AddNumWithOutPlus{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the first number: ");
int num1=scan.nextInt();//get input from the user for num1
System.out.print("Enter the second number: ");
int num2=scan.nextInt();//get input from the user for num2

while(num2 != 0){
int num3=(num1 & num2);
num1=num1^num2;
num2=num3<<1;
}
System.out.print("Sum of two numbers is: "+num1);
}
}

When the above code is executed it produces the following output

Enter the first number: 123
Enter the second number: 234
Sum of two numbers is: 357

 

Similar post

C program to addition of two numbers

C++ program to addition of two numbers

Python program to addition of two numbers

 

Suggested for you

For loop in Java language

While loop in Java language

Do-while loop in Java language

Data type and variable in java language

Operator in Java language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes