How to Add Two Numbers in Java
The addition is the fundamental aspect we use to learn programming in any language. In this article, we will learn different methods to add two numbers in java. So, without further delay letโs dive deep to learn how to add numbers.
In our earlier education days, we must learn addition, subtraction, division, and multiplication. We use the โ+โ operator to add two integers. Similarly, we use the โ+โ operator to add two numbers in Java. We say operands to these numbers because we do operations on operands. When we add two numbers, always remember that the size of the result should not extend the size of the data type.
Addition uses โ+โ operator on two numbers
a + b
Here, a and b are two operands.
Must Check: Introduction to Java Basics
Must Check: Variables in Java
Letโs look at the different ways by which we can add two numbers in Java.
- Addition of two numbers using the โ+โ operator
- Addition of two numbers in Java with User input
- The sum of two numbers in Java using the method
- By using for loop
Addition of two numbers using โ+โ operator
In the below code, we take two integers a =10 and b =20 and add these two numbers using โ+โ operator and store the result in sum which is the integer type.
Code:
public class Main{ public static void main(String args[]) { int a = 10; int b = 20; int sum = a + b; System.out.println("Addition of a and b is:"+ sum); }}
Output
Must Read: What is Java?
Must Check: Java Online Courses and Certification
Addition of two numbers in Java with User input
In the below code, we use nextInt() method of Scanner class to take input from user.
Code
import java.util.Scanner;
public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a first number:"); int a = sc.nextInt(); System.out.println("Enter a second number:"); int b = sc.nextInt(); int sum = a + b; System.out.println("Addition of a and b is:"+ sum); }}
Output:
Must Read: Understanding Java Scanner Class
The sum of two numbers in Java using a method
By using sum() method
Integer class in Java has a method sum(), which we use two add two numbers by passing arguments in the sum() method. Observe the code for better understanding.
Code:
import java.util.Scanner;
public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a first number:"); // Taking input of integer int a = sc.nextInt(); System.out.println("Enter a second number:"); // Taking input of integer int b = sc.nextInt(); // calling sum method by passing integer a and b int sum = Integer.sum(a, b); System.out.println("Addition of a and b is:"+ sum); }}
Output:
By using a user-defined method
In the below code, we make a static method sum that takes two integers as arguments and has return type int. This method adds two integers passed as arguments and returns the result. Observe the code.
Code:
import java.util.Scanner;
public class Main{ public static int sum( int a, int b) { int ans=a+b; return ans; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter a first number:"); // Taking input of integer int a = sc.nextInt(); System.out.println("Enter a second number:"); // Taking input from integer int b = sc.nextInt(); // calling sum method int sum = Main.sum(a, b); System.out.println("Addition of a and b is:"+ sum); }}
Output:
By using for loop
We can add numbers using for loop. In the below code, we first take input from the user on how many numbers to be added; suppose the user enters n numbers to be added, then we enter into for loop n times and take number input one by one and add the number to sum, which is initialized to zero. After the addition of all the numbers, we will return the sum. Observe the code for better understanding.
Code:
import java.util.Scanner;
public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter total numbers to be added:"); int n=sc.nextInt(); int sum=0; for(int i=0;i<n;i++) { System.out.println("Enter a number:"); int num=sc.nextInt(); sum+=num; } System.out.println("Sum of numbers:"+ sum); }}
Best-suited Java courses for you
Learn Java with these high-rated online courses
Special Case: When Integers are in String Form
Letโs take a case when integers are in String form and how we can add these integers. So, for this type of scenario in Java, we have some inbuilt methods in the Integer class. We can use Integer.parseInt() method to Integer, and then we typecast the Integer to int. Letโs take a program to understand this clearly. For example, we have String a and String b as
String a=โ400โณ;
String b=โ100โณ;
Code:
public class Main{ public static void main(String args[]) { String a="400"; String b="100"; //we use parseInt() method of Integer to convert a String value to Integer
// Converted Integer is type cast into int;
int val1=(int)Integer.parseInt(a); int val2=(int)Integer.parseInt(b);
int sum=val1+val2; System.out.println("Sum of a and b is:"+sum);
}}
Output
Conclusion
In this article, we have discussed different methods to add two numbers in Java with the help of examples.
Hope you will like the article.
Keep Learning!!
Keep Sharing!!
Contributed By: Shubham Kumar
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio