0

For my assignment, my program needs to ask the user which of the five functions he wants to execute. The five functions include:

  1. Summation of a number
  2. Factorial of a number
  3. Fibonacci value of the nth term.
  4. gcd of two numbers
  5. a to the power of b.

The user will be prompted repeatedly until he wishes to exit. All my functions work fine. However, i think i messed up on one of the loops because once i enter which function i want to execute and enter a value, it keeps displaying the answer in an infinite loop.

#include <stdio.h>
#include <math.h>

// function to find summation of a number
int summation(int k) {
  int i;

  for(i = k; i >= 0; i--) {
    k = i + (i-1);
  }
  return k;
}

// function to find the factorail of a number
int factorial(int num) {
  int i;

  for(i = num - 1; i > 0; i--) {
    num = num * i;
  }
  return num;
}

// dunxtion to find the fibonacci of the nth term
int fibonacci(int n){
  int i, t1 = 0, t2 = 1, nextTerm;

  for(i = 1; i <= n; i++) {
    if(i == 1) {
        printf("%d, ", t1);
        continue;
    }
    if(i == 2) {
        printf("%d, ", t2);
        continue;
    }
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
    printf("%d, ", nextTerm);
  }
  return nextTerm;
}

// function to find the gcd of two numbers
int gcd(int n, int m) {
  int i, gcd;

  for(i=1; i <= n && i <= m; i++) {
    // Checks if i is a factor of both integers
    if(n % i == 0 && m % i == 0)
        gcd = i;
  }
  return gcd;
}

// function to find value of n to the power of m
int power(int n, int m) {
  return pow(n, m);
}

int main(void) {;
  int option ,n, m;

//Asks user for what they want to find
  printf("If you would like to find the summation of a number, enter 1 \n");
  printf("If you would like to find the factorial of a number, enter 2 \n");
  printf("If you would like to find the fibonacci sequence of a number, enter 3 \n");
  printf("If you would like to find the gcd of two numbers, enter 4 \n");
  printf("If you would like to find the power of a number a to b, enter 5 \n");
  printf("If you would like to exit, enter 0 \n");
  scanf("%d", &option);

// Enables the program to prompt the user until they wish to exit
  while(option != 0) {  
    switch(option) { //If user wishes to find the summation
        case 1: if(option == 1) {   
                    printf("Enter a number: ");
                    scanf("%d", &n);
                    while(n > 0) {
                        if(n < 1) { //message displayed if an invalid value is entered
                            printf("invalid value");
                        }
                        else {
                            printf("Summation of %d is %d", n, summation(n));
                        }
                    }
                }
        case 2: if(option == 2) { //if user wishes to find factorial of a number
                    printf("Enter a number: ");
                    scanf("%d", &n);
                    while(n >= 0) {//message displayed if an invalid value is entered
                        if(n < 0) {
                            printf("invalid value");
                        }
                        else {
                            printf("factorial of %d is %d", n, factorial(n));
                        }
                    }
                }
        case 3: if(option == 3) { //if user wishes to find the fibonacci value of the nth term
                    printf("Enter a number: ");
                    scanf("%d", &n);
                    while(n >= 0) {//message displayed if an invalid value is entered
                        if(n < 0) {
                            printf("invalid value");
                        }
                        else {
                            printf("fibonacci of %d is %d", n, fibonacci(n));
                        }
                    }
                }
        case 4: if(option == 4) { 
                    printf("Enter a number: ");
                    scanf("%d %d", &n, &m);
                    while(n >= 0 && m >= 0) {
                        if(n < 0 || m < 0) {//message displayed if an invalid value is entered
                            printf("invalid value");
                        }
                        else {
                            printf("GCD of %d and %d is %d", n, m, gcd(n, m));
                        }
                    }
                }
        case 5: if(option == 5) {
                    printf("Enter a number: ");
                    scanf("%d %d", &n, &m);
                    while(n >= 0 && m >= 0) {
                        if(n <= 0 || m < 0) {
                            printf("invalid value");
                        }
                        else {
                            printf("%d to the power of %d is %d", n, m, power(n, m));
                        }
                    }
                }
        default: if(option == 0) {
            break;
        }
    }
    scanf("%d", &option);
}
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Shoaib Ahmed
  • 115
  • 2
  • 5

2 Answers2

0

First of all, C has unstructured switch statement.

You need to add a break; statement after each of your case body to limit the execution for a particular case to the body mentioned under that case.

Otherwise, by default, (with the absence of a break statement) all the case statements works in fall-through manner. You can read more about it here.


That said, regarding the repeated execution of a single function, there's a serious flaw in most (if not all) of the the logic. For example, let's take this

   while(n > 0) {
     if(n < 1) { //message displayed if an invalid value is entered
         printf("invalid value");
     }
     else {
         printf("Summation of %d is %d", n, summation(n));
     }
   }

here, you're replying on n becoming 0 at some point to break out of the loop, but you did not modify n, at all.

To elaborate, C uses pass-by-value for argument passing, so for the call, summation(n), inside the function, whatever change you make to the parameter receiving the value of n, is not reflected in the caller, and thus, the n in the caller remains unchanged.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Yeah i completely forgot about the break statements. But even after adding them in, it still has the same problem. – Shoaib Ahmed Jan 18 '17 at 18:10
  • @ShoaibAhmed you mean it still executes _all_ the functions? – Sourav Ghosh Jan 18 '17 at 18:13
  • All the functions are not executed. Let's say I enter 2 for variable "option" which finds the factorial of a an entered number. The function is executed properly. The only problem is that it keeps displaying the answer constantly in an infinite loop. – Shoaib Ahmed Jan 18 '17 at 18:16
  • Ahh.. I see, i get it now..wait for an update to answer. – Sourav Ghosh Jan 18 '17 at 18:18
0

You just need a break statement at the end of every case like:

 case 1: if(option == 1) { 
                printf("Enter a number: ");
                scanf("%d", &n);
                while(n > 0) {
                    if(n < 1) { //message displayed if an invalid value is entered
                        printf("invalid value");
                    }
                    else {
                        printf("Summation of %d is %d", n, summation(n));
                    }
                }
            } 
break;

As the control will fall down to next case if no break statement is present.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574