10 simple ways to add two numbers in Java
10 simple ways to add two numbers in Java
In this article, we will discuss the concept of the 10 simple ways to add two numbers in Java
In this post, we are going to learn how to find sum of two number using 10 ways(methods) in Java programming language
Methods to add two numbers
Using simple arithmetic operator
Here, we use a simple “+” operator to find the addition of two numbers
Program 1
// Addition of two numbers in Java // Simple Addition using method class Additio{ public static void main(String[] args) { int num1=60; int num2=40; System.out.println("sum: "+(num1+num2)); } }
When the above code is executed, it produces the following result
sum: 100
Using Java method
Here, we use a Java user defined method to find the addition of two numbers
Program 2
// Addition of two numbers in Java // Simple Addition using method class AdditionUsingMethod { public static void main(String[] args) { int num1=12; int num2=13; System.out.println("sum: "+add(num1,num2)); } public static int add(int a, int b){ return a+b; } }
When the above code is executed, it produces the following result
sum: 25
Using Java method with array
Here, we use a Java user defined method with array to find the addition of two numbers
Program 3
// Addition of two numbers in Java // Simple Addition using method with array class AdditionUsingMethod { public static void main(String[] args) { int num1=15; int num2=35; int[] result=new int[1]; add(num1,num2,result); System.out.println("sum: "+result[0]); } public static void add(int a, int b, int[] result){ result[0]=a+b; } }
When the above code is executed, it produces the following result
sum: 50
Using Scanner for user input
Here, we use a Java scanner class with user input to find the addition of two numbers
program 4
// Addition of two numbers in Java // Simple Addition Using Scanner for user input import java.util.Scanner; class AdditionUsingMethod { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter first number: "); int num1=sc.nextInt(); System.out.print("Enter first number: "); int num2=sc.nextInt(); System.out.println("sum: "+(num1+num2)); } }
When the above code is executed, it produces the following result
Enter first number: 23 Enter first number: 34 sum: 57
Using static method
Here, we use a static method to find the addition of two numbers
program 5
// Addition of two numbers in Java // Simple Addition using Static method import java.util.Scanner; class Static_Addition { public static void main(String[] args) { int num1=45; int num2=55; int sum=num1+num2; System.out.println("sum: "+add(num1,num2)); } public static int add(int a, int b){ return a+b; }}
When the above code is executed, it produces the following result
sum: 100
Using constructor
Here, we use a Java constructor to find the addition of two numbers
program 6
// Addition of two numbers in Java // Simple Addition using Constructor import java.util.Scanner; class Addition_Constructor { int sum; Addition_Constructor (int num1, int num2){ this.sum=num1+num2; } public static void main(String[] args) { int num1=45; int num2=55; Addition_Constructor add=new Addition_Constructor(num1,num2); System.out.println("sum: "+add.sum); } }
When the above code is executed, it produces the following result
sum: 1000
Using recursion
Here, we use a recursive method to find the addition of two numbers
program 7
// Addition of two numbers in Java // Simple Addition using recursion class Addition_Recursion { public static void main(String[] args) { int num1=35; int num2=25; int sum=add(num1,num2); System.out.println("sum: "+sum); } //the b is zero , the sum is a public static int add(int a, int b){ if(b==0){ return a; } //add one to aand subtract one from b return add(a+1,b-1); } }
When the above code is executed, it produces the following result
sum: 60
Using List
program 8
// Addition of two numbers in Java // Simple Addition using list import java.util.Arrays; import java.util.List; class Addition_List { public static void main(String[] args) { int num1=350; int num2=250; List<Integer> numbers=Arrays.asList(num1,num2); int sum=numbers.get(0)+numbers.get(1); System.out.println("sum: "+sum); } }
When the above code is executed, it produces the following result
sum: 600
Using Stream
program 9
// Addition of two numbers in Java // Simple Addition using Stream import java.util.stream.IntStream; class Addition_Stream { public static void main(String[] args) { int num1=50; int num2=20; int sum=IntStream.of(num1,num2).sum(); System.out.println("sum: "+sum); } }
When the above code is executed, it produces the following result
sum: 70
Using Integer.sum method
Here, we use a a pre- defined method (integer.sum) to find the addition of two numbers
program 10
// Addition of two numbers in Java // Simple Addition using integer.sum method import java.util.stream.IntStream; class Addition_IntSum_method { public static void main(String[] args) { int num1=55; int num2=25; int sum=Integer.sum(num1,num2); System.out.println("sum: "+sum); } }
When the above code is executed, it produces the following result
sum: 80
Similar post
Sum of two numbers in C language
Sum of two numbers in C++ language
Sum of two numbers in Python language
Sum of two numbers in Java language without + operator
Sum of two numbers in Java language using bitwise operator
Sum of two numbers in C language without + operator
Sum of two numbers in C language using bitwise operator
Calculate addition of two numbers using method in Java language
Calculate addition of two numbers using function in C language
Calculate addition of two numbers using function in C++ language
Calculate addition of two numbers using function in Python language