Java Program to Add Two Numbers
Last Updated :
25 Dec, 2023
Given two integers num1 and num2, the task is to find the sum of the given two numbers in Java.
Example of Addition of Two Numbers
Input: A = 5, B = 6
Output: sum = 11
Input: A = 4, B = 11Â
Output: sum = 15 Â
Program to Add Two Numbers in Java
Below is the implementation of adding two Numbers are mentioned below:
Java
import java.io.*;
class GFG {
public static int sum( int num1, int num2)
{
return num1+num2;
}
public static void main(String[] args)
{
GFG ob = new GFG();
int res = ob.sum( 28 , 49 );
System.out.println(res);
}
}
|
Using Bit Manipulation for Addition of Two Numbers in Java
Bitwise Operators are the operators that can directly operate on the bit values.
Below is the implementation of the above method:
Java
import java.io.*;
class GFG {
public static int sum( int num1, int num2)
{
if (num2 == 0 ) return num1;
return sum(num1 ^ num2, (num1 & num2) << 1 );
}
public static void main(String[] args)
{
GFG ob = new GFG();
int res = ob.sum( 28 , 49 );
System.out.println(res);
}
}
|
Sum of Three Numbers in Java
Sum of Three Numbers in itself is like Adding Two Numbers Two Times.
Example:
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int a= 1 ,b= 2 ,c= 3 ;
int k=a+b;
int result=k+c;
System.out.println( "Method 1: Sum a+b+c = " +result);
System.out.println( "Method 2: Sum a+b+c = " +(a+b+c));
}
}
|
Output
Method 1: Sum a+b+c = 6
Method 2: Sum a+b+c = 6
Sum of N Numbers in Java
Sum of N Numbers in Java can be done using the Steps mentioned below:
- Take input of N Numbers
- Use Loop to Add the N Numbers in Java(Iterate and Sum simultaneously)
- Print the Sum calculated.
Below is the implementation of the above method:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N;
Scanner sc = new Scanner(System.in);
System.out.println( "Enter the Number of elements:"
N = sc.nextInt();
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
int a = sc.nextInt();
sum += a;
}
System.out.println( "Sum of Input Numbers : " + sum);
}
}
|
Output:
Enter the Number of elements: 4
10
20
30
40
Sum of Input Numbers : 100
Miscellanous of Addition of Two Numbers in Java
These are the two Methods for Adding Two Numbers which are bit complex to implement as compared to the methods mentioned above.
1. Sum of Two Numbers Using Command Line Arguments in Java
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int a= Integer.parseInt(args[ 0 ]);
int b= Integer.parseInt(args[ 1 ]);
System.out.println( "Sum:" +(a+b));
}
}
|
Output
2. Program to Add Two BigIntegers in Java
BigIntegers are the numbers that exceed the limit of Integers So we created another class called BigInteger.
Below is the implementation of the Addition of Two BigIntegers:
Java
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger sum;
String input1 = "9482423949832423492342323546" ;
String input2 = "6484464684864864864864876543" ;
BigInteger a = new BigInteger(input1);
BigInteger b = new BigInteger(input2);
sum = a.add(b);
System.out.println( "Sum of Two BigIntegers: "
+ sum);
}
}
|
Output
Sum of Two BigIntegers: 15966888634697288357207200089
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...