Table of Contents
In this tutorial, we will discuss the Java program to 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
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
Method 2
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
Method 3
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
Method 4
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
Method 5
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
Do-while loop in Java language
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.