Java program to add two numbers
In this post, you will learn the addition of two numbers using the Java programming language. Such a type of basic program is useful to improve your logic building. There are different ways to add two numbers in Java. Here, we have mentioned most of them.
Simple Java program to add two numbers
Here is the simple Java program to add two numbers. In the given program, we specify the value of both the numbers in integer variables. Then, we add the both integers using the + operator and print the sum.
public class AddTwoNumbers {
public static void main(String[] args) {
int num1 = 85, num2 = 35, sum;
sum = num1 + num2;
System.out.println("Sum of given numbers: "+sum);
}
}
Output of the above code:
Sum of given numbers: 120
Java program to add two numbers using user input
In the given Java program, we have asked the user to enter two numbers and then calculate them.
// Import the Scanner class
import java.util.Scanner;
public class AddNumbers {
public static void main(String[] args) {
int num1, num2, sum;
// Create a Scanner object
// Read user input
Scanner myObj = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = myObj.nextInt();
System.out.println("Enter Second Number: ");
num2 = myObj.nextInt();
// Calculate the sum of num1 + num2
sum = num1 + num2;
System.out.println("Sum of numbers : " + sum);
}
}
Output of the above code:
Enter First Number: 39
Enter Second Number: 89
Sum of numbers : 128
Java program to add two numbers using user-defined function
In the given Java program, we have defined a function that takes two numbers as input, calculates the sum of the numbers using the user-defined method, and prints the result.
import java.util.Scanner;
public class AddNumbers
{
public static void main(String args[])
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
num1 = sc.nextInt();
System.out.print("Enter the second number: ");
num2 = sc.nextInt();
sum = sum(num1, num2);
System.out.println("The sum of two numbers num1 and num2 : " + sum);
}
//method that calculates the sum
public static int sum(int a, int b)
{
int sum = a + b;
return sum;
}
}
Output of the above code:
Enter First Number: 39
Enter Second Number: 89
Sum of numbers : 128
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java