Advertisement
Niloy007

Musa's Calculator assignment

Jun 1st, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int add(int a, int b) {
  4.     return a + b;
  5. }
  6.  
  7. int sub(int a, int b) {
  8.     return a - b;
  9. }
  10.  
  11. int mul(int a, int b) {
  12.  
  13.     return a * b;
  14. }
  15.  
  16. float div(int a, int b) {
  17.     return a / b;
  18. }
  19.  
  20. int mod(int a, int b) {
  21.     return a % b;
  22. }
  23.  
  24. int power(int a, int b) {
  25.     int res = 1;
  26.     while (b != 0) {
  27.         res *= a;
  28.         b--;
  29.     }
  30.     return res;
  31. }
  32.  
  33. int main() {
  34.     int a, b, choice, result;
  35.     while (true) {
  36.         printf("1. Addition\n");
  37.         printf("2. Subtraction\n");
  38.         printf("3. Multiplicaton\n");
  39.         printf("4. Division\n");
  40.         printf("5. Modulus\n");
  41.         printf("6. Power\n");
  42.         printf("7. Exit\n");
  43.         printf("Enter Your choice: ");
  44.         scanf("%d", &choice);
  45.         if (choice == 1) {
  46.             printf("Enter two numbers: ");
  47.             scanf("%d %d", &a, &b);
  48.             result = add(a, b);
  49.             printf("%d\n", result);
  50.         } else if (choice == 2) {
  51.             printf("Enter two numbers: ");
  52.             scanf("%d %d", &a, &b);
  53.             result = sub(a, b);
  54.             printf("%d\n", result);
  55.         } else if (choice == 3) {
  56.             printf("Enter two numbers: ");
  57.             scanf("%d %d", &a, &b);
  58.             result = mul(a, b);
  59.             printf("%d\n", result);
  60.         } else if (choice == 4) {
  61.             printf("Enter two numbers: ");
  62.             scanf("%d %d", &a, &b);
  63.             if (b != 0) {
  64.                 result = div(a, b);
  65.                 printf("%d\n", result);
  66.             } else {
  67.                 printf("Divide by zero is not possible!\n");
  68.             }
  69.         } else if (choice == 5) {
  70.             printf("Enter two numbers: ");
  71.             scanf("%d %d", &a, &b);
  72.             result = mod(a, b);
  73.             printf("%d\n", result);
  74.         } else if (choice == 6) {
  75.             printf("Enter two numbers: ");
  76.             scanf("%d %d", &a, &b);
  77.             result = power(a, b);
  78.             printf("%d\n", result);
  79.         } else if(choice == 7) {
  80.             break;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement