Java program to enter two numbers and find their sum
ADVERTISEMENTS
Java program to demonstrate find the sum of two integer values
// Java program to demonstrate find the sum of two integer values
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter two integer values::\n");
// Reading integer data using nextInt
int p = in.nextInt();
int q = in.nextInt();
int r;
r = p + q;
System.out.println("\n\nResult:: " + p + " + " + q + " = " + r);
}
}
Output:
Enter two integer values::
5
7
Result:: 5 + 7 = 12
Java program to demonstrate find the sum of two integer values with minimum variables
// Java program to demonstrate find the sum of two integer values with minimum variables
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter two integer values::\n");
// Reading integer data using nextInt
int p = in.nextInt();
int q = in.nextInt();
System.out.print("\n\nResult:: " + p + " + " + q + " = ");
p = p + q;
System.out.println(p);
}
}
Output:
Enter two integer values::
5
7
Result:: 5 + 7 = 12
Java program to demonstrate find the sum of two float values
// Java program to demonstrate find the sum of two float values
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter two float values::\n");
// Reading float data using nextFloat
float p = in.nextFloat();
float q = in.nextFloat();
System.out.print("\n\nResult:: " + p + " + " + q + " = ");
p = p + q;
System.out.println(p);
}
}
Output:
Enter two float values::
5.23
7.29
Result:: 5.23 + 7.29 = 12.52