Java Program to Add Two Numbers (original) (raw)
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 = 11Input: 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 `
// Java Program to implement // Direct Addition to Add two Numbers import java.io.*;
// Driver Class class GFG { public static int sum(int num1, int num2) { return num1+num2; }
// main function
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 `
// Java Program to implement // Bit Manipulation to Find // the Sum of two Numbers import java.io.*;
// Driver Class class GFG { // function to find sum public static int sum(int num1, int num2) { if (num2 == 0) return num1; return sum(num1 ^ num2, (num1 & num2) << 1); }
// main function
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 `
// Java Program to Sum of Three Numbers import java.io.*;
class GFG { public static void main (String[] args) { // Three Integers int a=1,b=2,c=3;
// Method 1:
// Adding sum of a+b and then
// sum of a+b with c
int k=a+b;
int result=k+c;
// Printing to Sum
System.out.println("Method 1: Sum a+b+c = "+result);
// Method 2:
// Printing the Sum of Three
// Variables(Numbers)
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 `
// Java Program to Add N Numbers import java.io.; import java.util.;
// Driver Class class GFG { // main function public static void main(String[] args) {
// N is the number if elements
int N;
// Initialising the Scanner Class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number of elements:"
// Taking the input of N
N = sc.nextInt();
int sum = 0;
// Taking N inputs and find the sum
for (int i = 0; i < N; i++) {
int a = sc.nextInt();
sum += a;
}
// Printing the sum of N numbers
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 `
// Java Program to find // Sum of Two Numbers Using // Command Line import java.io.*;
// Driver Class class GFG { // main function public static void main (String[] args) {
// Command Line Input
// Stored in Variables
int a= Integer.parseInt(args[0]);
int b= Integer.parseInt(args[1]);
// Printing Sum of variables
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 `
// Java program to demonstrate // add() method of BigInteger import java.math.BigInteger;
// Driver Class public class GFG { // main function public static void main(String[] args) { // BigInteger object to store result BigInteger sum;
// For user input
// Use Scanner or BufferedReader
// Two objects of String created
// Holds the values to calculate the sum
String input1 = "9482423949832423492342323546";
String input2 = "6484464684864864864864876543";
// Convert the string input to BigInteger
BigInteger a = new BigInteger(input1);
BigInteger b = new BigInteger(input2);
// Using add() method
sum = a.add(b);
// Display the result in BigInteger
System.out.println("Sum of Two BigIntegers: "
+ sum);
}
}
`
Output
Sum of Two BigIntegers: 15966888634697288357207200089