Advertisement
jackiebinya

fxn call in switch case.c

Jun 4th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void ft_modulus (int n1, int n2);
  5. void ft_multiplication (int n1, int n2);
  6. void ft_division (int n1, int n2);
  7.  
  8. int main()
  9. {
  10. printf("#####****MATHS CHECKER****######\n");
  11. //user prompt
  12. printf("MENU\n");
  13. //menu options
  14. printf("1 => MODULUS\n");
  15. printf("2 => MULTIPLICATION\n");
  16. printf("3 => DIVISION\n");
  17. printf("4 => EXIT\n");
  18.  
  19. int num1, num2;
  20. //user prompt
  21. printf("Enter 2 random numbers.\n");
  22. scanf("%d%d", &num1, &num2);
  23.  
  24. int option;
  25. //user prompt
  26. printf("Choose an operation from menu above to be performed on the numbers:\n");
  27. scanf("%d", &option);
  28.  
  29. switch (option)
  30. {
  31. case 1: ft_modulus(num1, num2);
  32. break;
  33.  
  34. case 2: ft_multiplication(num1, num2);
  35. break;
  36.  
  37. case 3: ft_division(num1, num2);
  38. break;
  39.  
  40. case 4: break;
  41.  
  42. }
  43.  
  44.  
  45. return 0;
  46. }
  47.  
  48. void ft_modulus(int n1, int n2)
  49. {
  50. int correctAnswer, userAnswer, newAnswer;
  51.  
  52. correctAnswer = n1%n2;
  53.  
  54. //user prompt
  55. printf("Enter answer you got:\n");
  56. scanf("%d", &userAnswer);
  57.  
  58. if (userAnswer == correctAnswer)
  59. {
  60. printf("Congrats!!!Your answer is correct.\n");
  61. }
  62.  
  63. else if (userAnswer != correctAnswer)
  64. {
  65. printf("Incorrect:(Please try again.\n");
  66. printf("Enter new answer:\n");
  67. scanf("%d", &newAnswer);
  68.  
  69. if (newAnswer == correctAnswer)
  70. {
  71. printf("Congrats!!!Your answer is correct.\n");
  72. }
  73.  
  74. else if (newAnswer != correctAnswer);
  75. {
  76. printf("Incorrect:(You have exhausted all your chances.\n");
  77. printf("%d %% %d = %d", n1, n2, correctAnswer);
  78. }
  79. }
  80.  
  81. }
  82.  
  83. void ft_multiplication (int n1, int n2)
  84. {
  85. int correctAnswer, userAnswer, newAnswer;
  86.  
  87. correctAnswer = n1*n2;
  88.  
  89. //user prompt
  90. printf("Enter answer you got:\n");
  91. scanf("%d", &userAnswer);
  92.  
  93. if (userAnswer == correctAnswer)
  94. {
  95. printf("Congrats!!!Your answer is correct.\n");
  96. }
  97.  
  98. else if (userAnswer != correctAnswer)
  99. {
  100. printf("Incorrect:(Please try again.\n");
  101. printf("Enter new answer:\n");
  102. scanf("%d", &newAnswer);
  103.  
  104. if (newAnswer == correctAnswer)
  105. {
  106. printf("Congrats!!!Your answer is correct.\n");
  107. }
  108.  
  109. else if (newAnswer != correctAnswer);
  110. {
  111. printf("Incorrect:(You have exhausted all your chances.\n");
  112. printf("%d * %d = %d", n1, n2, correctAnswer);
  113. }
  114. }
  115.  
  116. }
  117.  
  118. void ft_division (int n1, int n2)
  119. {
  120. int correctAnswer, userAnswer, newAnswer;
  121.  
  122. correctAnswer = n1/n2;
  123.  
  124. //user prompt
  125. printf("Enter answer you got:\n");
  126. scanf("%d", &userAnswer);
  127.  
  128. if (userAnswer == correctAnswer)
  129. {
  130. printf("Congrats!!!Your answer is correct.\n");
  131. }
  132.  
  133. else if (userAnswer != correctAnswer)
  134. {
  135. printf("Incorrect:(Please try again.\n");
  136. printf("Enter new answer:\n");
  137. scanf("%d", &newAnswer);
  138.  
  139. if (newAnswer == correctAnswer)
  140. {
  141. printf("Congrats!!!Your answer is correct.\n");
  142. }
  143.  
  144. else if (newAnswer != correctAnswer);
  145. {
  146. printf("Incorrect:(You have exhausted all your chances.\n");
  147. printf("%d / %d = %d", n1, n2, correctAnswer);
  148. }
  149. }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement