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
// 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));
}
}
OutputMethod 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);
}
}
OutputSum of Two BigIntegers: 15966888634697288357207200089
Similar Reads
Java Program to Add two Complex Numbers Complex numbers are numbers that consist of two parts â a real number and an imaginary number. Complex numbers are the building blocks of more intricate math, such as algebra. The standard format for complex numbers is a + bi, with the real number first and the imaginary number last. General form fo
3 min read
Java Program to Add Two Binary Strings When two binary strings are added, then the sum returned is also a binary string. Example: Input : x = "10", y = "01" Output: "11"Input : x = "110", y = "011" Output: "1001" Explanation: 110 + 011 =1001Approach 1: Here, we need to start adding from the right side and when the sum returned is more th
3 min read
Java Program to Find Average of Two Lists To calculate the average of two lists in Java we first need to combine the two lists into one. we can do this using the addAll() method of the ArrayList class. Once you have combined the lists we can calculate the average by summing up all the elements in the combined list and dividing by the total
2 min read
Java Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input:Â List1: 5->6->3 // represents number 563Â List2: 8->4->2 // represents number 842Â Output:Â Resultant list: 1->4->0->5 // re
4 min read
Java Program to Add Two numbers Without using Arithmetic Operator Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, â, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the '+' operator and thereby simply prin
2 min read
Java Program For Adding Two Numbers Represented By Linked Lists- Set 2 Given two numbers represented by two linked lists, write a function that returns the sum list. The sum list is linked list representation of the addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion). Example : Input:
8 min read
Java Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to
6 min read
Java Program To Multiply Two Numbers Represented By Linked Lists Given two numbers represented by linked lists, write a function that returns the multiplication of these two linked lists. Examples: Input: 9->4->6 8->4 Output: 79464 Input: 3->2->1 1->2 Output: 3852Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.
3 min read
Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
4 min read
Java Program to Convert Double to String The primary goal of double to String conversion in Java is to store big streams of numbers that are coming where even data types fail to store the stream of numbers. It is generically carried out when we want to display the bigger values. In this article, we will learn How to Convert double to Strin
3 min read