Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 11. Math operations
- Write a method that receives two number and an operator, calculates the result and returns it. You will be given three lines of input. The first will be the first number, the second one will be the operator and the last one will be the second number. The possible operators are: / * + -
- Example
- Input Output
- 5
- *
- 5 25
- 4
- +
- 8 12
- Hint
- 1. Read the inputs and create a method that returns a double (the result of the operation)
- using System;
- namespace _11MathOperations
- {
- class Program
- {
- static void Main(string[] args)
- {
- var number1 = double.Parse(Console.ReadLine());
- var operators = char.Parse(Console.ReadLine());
- var number2 = double.Parse(Console.ReadLine());
- double result = MathOperations(number1, operators, number2);
- Console.WriteLine(result);
- }
- private static double MathOperations(double number1, char operators, double number2)
- {
- double result = 0;
- if (operators == '/')
- {
- result = number1 / number2;
- }
- else if (operators == '*')
- {
- result = number1 * number2;
- }
- else if (operators == '+')
- {
- result = number1 + number2;
- }
- else if (operators == '-')
- {
- result = number1 - number2;
- }
- return result;
- }
- }
- }
Add Comment
Please, Sign In to add comment