Table of Contents
.Java program to addition of two numbers
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
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
- Integer variable num1,num2 are declared and initialized
- the num1 and num 2 both are added together and the value is added to the sum
- 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
- Declare variables num1,num2,sum
- The program requires input from the user
- Then the user enters the input value for num1, num2(variables)
- The program will read the input using Scanner class and stores in the variables num1 and num2 respectively
- the num1 and num 2 both are added together and the value is added to the sum
- 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
- Declare variables num1,num2,sum
- The program requires input from the user
- Then the user enters the input value for num1, num2
- The program will read the input using Scanner class and store in the variables num1 and num2 respectively
- Define the method (addition()) for find sum
- Call the method to produced output
- 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
- Declare variables num1,num2,sum
- The program requires input from the user
- Then the user enters the input value for num1, num2
- The program will read the input using Scanner class and store in the variables num1 and num2 respectively
- Define the method (addition()) for find sum recursively
- Call the method to produced output
- 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
Do-while loop in Java language