Alexander_B

Operations Between Numbers

Feb 3rd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DomashnoOperationBetweenNumbers
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // get input --> int n1 and n2; char - +, -, *, /, %;
  14. // test if n2 == 0; if true print -> "Cannot divide {N1} by zero"
  15. // calculate result
  16. // decide if the result is even or odd
  17. // print --> + - * -> "{N1} {оператор} {N2} = {резултат} – {even/odd}"; / -> "{N1} / {N2} = {резултат}"; % -> "{N1} % {N2} = {остатък}"
  18.  
  19. int n1 = int.Parse(Console.ReadLine());
  20. int n2 = int.Parse(Console.ReadLine());
  21. string operation = Console.ReadLine();
  22.  
  23. if (n2 == 0 && (operation == "/" || operation == "%"))
  24. {
  25. Console.WriteLine($"Cannot divide {n1} by zero");
  26. }
  27. else
  28. {
  29. double result = 0.00;
  30. string evenOrOdd = "0";
  31.  
  32. if (operation == "+")
  33. {
  34. result = n1 + n2;
  35. double evenOrOddcalc = result % 2;
  36. if (evenOrOddcalc == 0)
  37. {
  38. evenOrOdd = "even";
  39. }
  40. else
  41. {
  42. evenOrOdd = "odd";
  43. }
  44. }
  45. else if (operation == "-")
  46. {
  47. result = n1 - n2;
  48. double evenOrOddcalc = result % 2;
  49. if (evenOrOddcalc == 0)
  50. {
  51. evenOrOdd = "even";
  52. }
  53. else
  54. {
  55. evenOrOdd = "odd";
  56. }
  57. }
  58. else if (operation == "*")
  59. {
  60. result = n1 * n2;
  61. double evenOrOddcalc = result % 2;
  62. if (evenOrOddcalc == 0)
  63. {
  64. evenOrOdd = "even";
  65. }
  66. else
  67. {
  68. evenOrOdd = "odd";
  69. }
  70. }
  71. else if (operation == "/")
  72. {
  73. result = 1.00 * n1 / n2;
  74. }
  75. else
  76. {
  77. result = n1 % n2;
  78. }
  79.  
  80. if (operation == "+")
  81. {
  82. Console.WriteLine($"{n1} {operation} {n2} = {result} - {evenOrOdd}");
  83. }
  84. else if (operation == "-")
  85. {
  86. Console.WriteLine($"{n1} {operation} {n2} = {result} - {evenOrOdd}");
  87. }
  88. else if (operation == "*")
  89. {
  90. Console.WriteLine($"{n1} {operation} {n2} = {result} - {evenOrOdd}");
  91. }
  92. else if (operation == "/")
  93. {
  94. Console.WriteLine($"{n1} / {n2} = {result:f2}");
  95. }
  96. else // operation == "%"
  97. {
  98. Console.WriteLine($"{n1} % {n2} = {result}");
  99. }
  100. }
  101. }
  102. }
  103. }
Add Comment
Please, Sign In to add comment