How to add two numbers in Java?

In this tutorial we will learn How to add two numbers in Java?

How to add two numbers in a Java program?

In Java you can easily add two number by using the plus (+) operator. The plus operator will add two numbers in Java it will return you int value as sum of two numbers. In real world programming you can make methods to add the numbers.

In this tutorial we are going to define two numbers and then use the plus operator for adding two numbers. The value will be returned as sum of the number.

So, we have to define two variables and then calculate the sum of it.

Description this program

In this example we are defining two in variables:

int x = 10;
int y= 20;

Then to calculate the sum we have used following code:

int sum= x+y;

Here is the code of this program:

package net.roseindia;

public class AddTwoNumbers {

	public static void main(String[] args) {
		int x = 10;
		int y= 20;
		int sum= x+y;
		System.out.println("Sum is: "+sum);

	}

}

If you run the program this will print:

Sum is: 30

So, in this program you learned how to sum(add) two numbers in Java.

Check more tutorials at: Java Conversion examples.

Share on Google+Share on Google+

How to add two numbers in Java?

Posted on: October 24, 2017 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Addition of two Number in Class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads