In this core java programming tutorial we will Write a program to add two numbers in java.
Program / Example 1 to add two numbers in java>
/*
* Write a program to add two numbers in java
*/
public class AddTwoNumbersInJava {
public static void main(String[] args) {
int firstNumber = 1;
int secondNumber = 2;
System.out.println(firstNumber + " + "+secondNumber +" = " + (firstNumber + secondNumber));
}
}
/*OUTPUT
1 + 2 = 3
*/
|
Program / Example 2 to add two numbers in java, accept user's input >
import java.util.Scanner;
/*
* Write a program to add two numbers in java, user must be able to enter the number
*/
public class AddTwoNumbersInJavaFromUsersInput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("enter first Number : ");
int firstNumber = scan.nextInt();
System.out.print("enter second Number : ");
int secondNumber = scan.nextInt();
int sum = firstNumber + secondNumber;
System.out.println(firstNumber + " + "+secondNumber +" = " + sum);
}
}
/*OUTPUT
enter first Number : 1
enter second Number : 2
1 + 2 = 3
*/
|
So, in this core java programming tutorial we wrote a program to add two numbers in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>