How to Add Two Numbers in Java

How to Add Two Numbers in Java

3 mins read971 Views Comment
Updated on Mar 13, 2024 14:23 IST

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.

2023_02_MicrosoftTeams-image-200.jpg

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 โ€˜+โ€™ 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);
}
}
Copy code

Output

2023_02_image-195.jpg

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

Output:

2023_02_image-196.jpg

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

Output:

2023_02_image-197.jpg

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

Output:

2023_02_image-198.jpg

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

Output

2023_03_image.jpg

Must Read: Loops in Java

Must Read: Mastering For Loop in Java

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

โ€“ / โ€“
350 hours
Free
6 months
โ€“ / โ€“
4 months
โ€“ / โ€“
โ€“ / โ€“
โ€“ / โ€“
1 month
โ‚น50 K
7 weeks
โ‚น7.8 K
3 months
โ‚น8.47 K
2 months
โ‚น7 K
6 months
โ‚น4 K
2 months

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

Output

2023_02_image-200.jpg

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

About the Author

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