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
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
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
Suggested for you
Data types and variable in Java language
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…