Java program to add two numbers, a user enters two integers, and we calculate their sum and display it.
Example:
package javaapplication;
import java.util.Scanner;
/**
*
* @author Rathorji
*/
public class JavaApplication {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int p, q, r;
System.out.println("Enter two integers to calculate their sum: ");
Scanner sc = new Scanner(System.in); //keyboard input .
p = sc.nextInt(); //scan data for input of integer value
q = sc.nextInt();
r = p + q;
System.out.println("Sum of entered integers = " + r);
}
}
output:
run:
Enter two integers to calculate their sum: 10 20 Sum of entered integers = 30 |