Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 3. Calculations
- 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)
- Example
- Input Output
- subtract
- 5
- 4 1
- divide
- 8
- 4 2
- using System;
- namespace _03MethodsLabCalculations
- {
- class Program
- {
- static void Main(string[] args)
- {
- string command = Console.ReadLine();
- var a = int.Parse(Console.ReadLine());
- var b = int.Parse(Console.ReadLine());
- switch (command)
- {
- case "add":
- Add(a, b);
- break;
- case "multiply":
- Multiply(a, b);
- break;
- case "subtract":
- Subtract(a, b);
- break;
- case "divide":
- Divide(a, b);
- break;
- }
- }
- static void Add(int a, int b)
- {
- Console.WriteLine(a + b);
- }
- static void Multiply(int a, int b)
- {
- Console.WriteLine(a * b);
- }
- static void Subtract(int a, int b)
- {
- Console.WriteLine(a - b);
- }
- static void Divide(int a, int b)
- {
- Console.WriteLine(a / b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment