Advertisement
virtualideaz

OOD-JAVA program #2

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