Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.Create a program that will ask the user to enter the first number and second number. The program should give the sum and the difference of the 1st and 2nd number.
- Example:
- Enter the first number :
- Enter the second number :
- Sum :
- Difference :
- Source Code :
- import java.util.Scanner;
- public class SumAndDifference {
- static Scanner sc = new Scanner(System.in);
- public static void main(String [] args)
- {
- //the user will declare two numbers, so we have to create variables first.
- double N1;
- double N2;
- //variables are created
- //the program will ask the user to enter the first number
- System.out.print("Enter the first number : ");
- N1 = sc.nextDouble();
- //the N1 has been declared
- //the program will ask the user to enter the second number
- System.out.print("Enter the second number : ");
- N2 = sc.nextDouble();
- //both variables has been declared
- //the program will compute the two numbers
- double sum = N1 + N2;
- double difference = N1 - N2;
- //output of the program
- System.out.println("Sum : " + sum);
- System.out.println("Difference : " + difference);
- }
- }
- OUTPUT:
- Enter the first number : 17
- Enter the second number : 15
- Sum : 32.0
- Difference : 2.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement