Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Program to Add Two Numbers
Next article icon

Java Program to Add Two Numbers

Last Updated : 25 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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);     } } 

Output
77

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);     } } 

Output
77

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

Adding_Two_Numbers_Using_Command_Line

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

Next Article
Java Program to Add Two Numbers

A

aeroabrar_31
Improve
Article Tags :
  • Java
  • Java Programs
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences