Programs
Business Analytics

# How to Add Two Numbers in Java?

Given below is the flowchart depicting the addition of two numbers. The process is very simple. We are supposed to input two numbers and add them up. Save the answer in Sum and display the result.

Image Source: Google

Now we will take the next jump and see two programs for the addition of two numbers in Java. In the first example, we assign a value of the two numbers within the code. The second program takes the two numbers passed by the user and displays the sum.

Check out our free technology courses to get an edge over the competition.

Table of Contents

## Example 1: Finding the sum of two numbers

Output:

Image Source: Google

If we observe in the above-given program, we’ll find out that the values of the numbers are specified in the program.

Check Out upGrad’s Software Development Courses

## Example 2: Use of Scanner to compute the sum of two numbers

### What is a scanner?

The scanner helps us to capture the input given by the user so that the values of the two numbers can be obtained from the user. Then, the program computes the sum and shows it as output.

Image Source: Google

## Explore our Popular Software Engineering Courses

 Master of Science in Computer Science from LJMU & IIITB Caltech CTME Cybersecurity Certificate Program Full Stack Development Bootcamp PG Program in Blockchain Executive PG Program in Full Stack Development View All our Courses Below Software Engineering Courses

If we see in the above program, we’ll find that at the end of the program, both the numbers are required to be furnished by the user and then the sum is calculated and displayed at the end.

## Third Example: Sum of two numbers Using command line arguments

The aforementioned method is not the only way to add two numbers. You can add two number using command line arguments as well. Check out the program given below to learn and understand how to add two numbers with command line arguments:

Image Source : Google

## In-Demand Software Development Skills

 JavaScript Courses Core Java Courses Data Structures Courses Node.js Courses SQL Courses Full stack development Courses NFT Courses DevOps Courses Big Data Courses React.js Courses Cyber Security Courses Cloud Computing Courses Database Design Courses Python Courses Cryptocurrency Courses

Output :

Image Source: Google

## Read our Popular Articles related to Software Development

 How to Implement Data Abstraction in Java? What is Inner Class in Java? Java Identifiers: Definition, Syntax, and Examples Understanding Encapsulation in OOPS with Examples Command Line Arguments in C Explained Top 10 Features & Characteristics of Cloud Computing in 2022 Polymorphism in Java: Concepts, Types, Characterisitics & Examples Packages in Java & How to Use Them? Git Tutorial For Beginners: Learn Git from Scratch

If you want to learn how to add two numbers in Java, you can use the aforementioned methods. There are other methods as well that you can use to do the same, like Using Buffered Reader, Using Method, Without Using Addition+ Operator, & Using Classes and Objects.

At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development.

Want to share this article?

## Explore Free Courses

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!