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

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

 

Suggested for you

Operator in Java language

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

 

 

Cpp program to sum two numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago