Advertisement
virtualideaz

SumAndDifference.java

Mar 26th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. 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. Name the program, Numbers.java
  2. Example:
  3.  
  4. Enter the first number:
  5. Enter the second number:
  6. Sum:
  7. Difference :
  8.  
  9. My answer:
  10.  
  11. import java.util.Scanner;
  12. public class SumAndDifference {
  13. static Scanner sc = new Scanner(System.in);
  14. public static void main(String [] args)
  15.     {
  16.        //the user will declare two numbers, so we have to create variables first.
  17.  
  18. double N1;
  19. double N2;
  20.  
  21.        //variables are created
  22.        //the program will ask the user to enter the first number
  23.  
  24. System.out.print("Enter the first number : ");
  25.         N1 = sc.nextDouble();
  26.  
  27.         //the N1 has been declared
  28.         //the program will ask the user to enter the second number
  29.  
  30. System.out.print("Enter the second number : ");
  31.         N2 = sc.nextDouble();
  32.         //both variables has been declared
  33.  
  34.         //the program will compute the two numbers
  35. double sum = N1 + N2;
  36. double difference = N1 - N2;
  37.  
  38.        //output of the program
  39. System.out.println("Sum : " + sum);
  40. System.out.println("Difference : " + difference);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement