Advertisement
IvetValcheva

06. Operations Between Numbers

Jan 23rd, 2022
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _06._Operations_Between_Numbers
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int num1 = int.Parse(Console.ReadLine());
  10.             int num2 = int.Parse(Console.ReadLine());
  11.             char operation = char.Parse(Console.ReadLine());
  12.  
  13.             double result = 0;
  14.  
  15.             if(operation=='+' || operation == '-' || operation == '*')
  16.             {
  17.                 if(operation == '+')
  18.                 {
  19.                     result = num1 + num2;
  20.                 }
  21.                 else if (operation == '-')
  22.                 {
  23.                     result = num1 - num2;
  24.                 }
  25.                 else
  26.                 {
  27.                     result = num1 * num2;
  28.                 }
  29.  
  30.                 string evenOrOdd = "odd";
  31.                 if (result % 2 == 0)
  32.                 {
  33.                     evenOrOdd = "even";
  34.                 }
  35.  
  36.                 Console.WriteLine($"{num1} {operation} {num2} = {result} - {evenOrOdd}");
  37.             }
  38.             else //operation='/' || operation="%"
  39.             {
  40.                 if (num2 == 0)
  41.                 {
  42.                     Console.WriteLine($"Cannot divide {num1} by zero");
  43.                 }
  44.                 else
  45.                 {
  46.                     if(operation == '/')
  47.                     {
  48.                         result = 1.0 * num1 / num2;
  49.                         Console.WriteLine($"{num1} {operation} {num2} = {result:f2}");
  50.                     }
  51.                     else //operation="%"
  52.                     {
  53.                         result = num1 % num2;
  54.                         Console.WriteLine($"{num1} {operation} {num2} = {result}");
  55.                     }
  56.  
  57.                    
  58.                 }
  59.             }
  60.  
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement