Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
Java Program to Add Two Numbers (Java Sum / Addition)
In Java programming, solving numerical challenges lies at the core of a developer's journey. From simple arithmetic operations to complex mathematical algorithms, the ability to manipulate numbers efficiently forms the bedrock of coding.
Here, we will focus on a fundamental task: finding the sum of two numbers in Java. While straightforward, this task lays the groundwork for grasping essential concepts of Java data types, variables, and basic syntax.
We will take a step-by-step approach, walking you through the process of writing a Java program to add two numbers. Furthermore, we will explore different scenarios, including obtaining user input through command-line arguments or using the Scanner class for interactive input, showing the versatility of Java in handling various use cases.
Java Concepts to Learn for This Program:
Basic Java Program to Add Two Numbers
Code
class Main {
public static void main(String[] args) {
int first = 93;
int second = 21;
// sum of two numbers in Java
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}
Output
The sum is: 114
Explanation
The program starts with the declaration of a class named Main.
It has the signature public static void main(String[] args), where:
-
public indicates that the method is accessible from outside the class,
-
static means it belongs to the class rather than an instance,
-
void means the method doesn't return any value,
-
String[] args is an array of strings used to pass command-line arguments to the program.
Inside the main method, two integer variables first and second are declared and assigned values 93 and 21, respectively. The variables are used to store the two numbers whose sum is to be calculated.
The program then calculates the sum of the two numbers using the + operator. The result is stored in a new integer variable sum.
Addition of Two Numbers in Java Using Scanner
Here's a Java program to find the sum of two numbers using the scanner (with user input):
Code
import java.util.Scanner;
public class SumOfTwoNumbers {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the first number
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
// Prompt the user to enter the second number
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
// Calculate the sum of the two numbers
int sum = num1 + num2;
// Display the result
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
// Close the Scanner object
scanner.close();
}
}
Output
Enter the first number: 56
Enter the second number: 78
The sum of 56 and 78 is: 134
Explanation
-
We start by importing the java.util.Scanner class, which allows us to read input from the user.
-
In the main method, we create a Scanner object named scanner to read user input.
-
We use the nextInt() method of the Scanner class to read two integers entered by the user and store them in variables num1 and num2.
-
The sum of the two numbers is calculated and stored in the sum variable.
-
Finally, we display the result using the System.out.println() method.
Learn Next: