JulianJulianov

03.MethodsLab-Calculations

Feb 11th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. 3. Calculations
  2. Write a program that receives a string on the first line (add, multiply, subtract, divide) and on the next two lines receives two numbers. Create four methods (for each calculation) and invoke the right one depending on the command. The method should also print the result (needs to be void)
  3. Example
  4. Input        Output
  5. subtract
  6. 5
  7. 4            1
  8. divide
  9. 8
  10. 4            2
  11.  
  12. using System;
  13.  
  14. namespace _03MethodsLabCalculations
  15. {
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             string command = Console.ReadLine();
  21.             var a = int.Parse(Console.ReadLine());
  22.             var b = int.Parse(Console.ReadLine());
  23.  
  24.             switch (command)
  25.             {
  26.                 case "add":
  27.                     Add(a, b);
  28.                     break;
  29.                 case "multiply":
  30.                     Multiply(a, b);
  31.                     break;
  32.                 case "subtract":
  33.                     Subtract(a, b);
  34.                     break;
  35.                 case "divide":
  36.                     Divide(a, b);
  37.                     break;
  38.             }
  39.         }
  40.         static void Add(int a, int b)
  41.         {
  42.             Console.WriteLine(a + b);
  43.         }
  44.         static void Multiply(int a, int b)
  45.         {
  46.             Console.WriteLine(a * b);
  47.         }
  48.         static void Subtract(int a, int b)
  49.         {
  50.             Console.WriteLine(a - b);
  51.         }
  52.         static void Divide(int a, int b)
  53.         {
  54.             Console.WriteLine(a / b);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment