- On
- By
- 0 Comment
- Categories: addition, Calculations
Java program to sum of two numbers
Java program to the sum of two numbers
In this tutorial, we will discuss the Java program to the sum of two numbers
In this topic, we will learn how to add two numbers in Java programming language
Sum of two numbers
public class Add_Two_Numbers{ public static void main(String args[]){ int num_1=23,num_2=32,sum; //declare variables sum=num_1+num_2; //find total of two numbers(num_1,num_2) System.out.println("Sum of given numbers: "+sum); //display sum of numbers } }
When the above code is executed, it produces the following result
Sum of given numbers: 55
Sum of two numbers using the Scanner class
In this program, the user asked to enter two integer values. these two integers are stored in variables num_1 and num_2 respectively then two numbers are added using + operator and the result is stored in the sum variable
import java.util.Scanner; public class Add_Two_Numbers1{ public static void main(String args[]){ int sum; Scanner sc=new Scanner(System.in); System.out.print("Enter the first number: "); int num_1=sc.nextInt(); //get input from user for num_1 System.out.print("Enter the second number: "); int num_2=sc.nextInt(); //get input from user for num_2 sc.close(); sum=num_1+num_2; //calculate total System.out.print("Sum of given numbers: "+sum);//display total } }
In the above program, two integer values 45,65 (get input from the user using Scanner class) are stored in num_1 and num_2. Then num_1 and num_2 are added using the plus(+) operator.
Then the result is stored in another variable sum.
Finally, the result is displayed on the screen using display statements in Java
When the above code is executed, it produces the following result
Enter the first number: 45 Enter the first number: 65 Sum of given numbers: 110
Similar post
Sum of two numbers in C language
Sum of two numbers in C++ language
Sum of two numbers in Python language
Sum of two numbers in Java language without + operator
Sum of two numbers in Java language using bitwise operator
Sum of two numbers in C language without + operator
Sum of two numbers in C language using bitwise operator
Suggested for you
C++ program to multiply two numbers using function
Java program to multiply two numbers using method
Python program to multiply two numbers using function
C# program to multiply two numbers using function
PHP program to multiply two numbers using function
C program to multiply two numbers
C++ program to multiply two numbers
Java program to multiply two numbers
Python program to multiply two numbers
C# program to multiply two numbers
PHP program to multiply two numbers
JavaScript program to multiply two numbers
Calculate the total of array elements in Java
Calculate the total of array elements in C
Calculate the total of array elements in C++
Java program to calculate sum of array elements
C program to calculate sum of array elements
Java Program to calculate average of array
C Program to calculate average of array
Java Program to calculate average of odd and even numbers in an array
Java Program to calculate average of odd and even numbers
C Program to calculate average of odd and even numbers in an array
C Program to calculate average of odd and even numbers
C++ Program to calculate average of odd and even numbers in an array
C++ Program to calculate average of odd and even numbers
Data types and variable in Java
Find product of two numbers using recursion in Java
Data types and variable in Java language