Hi All,
I'm wondering if anyone can tell me where to put the "break" statement in the code below. I'm trying to make it so the program will keep asking a user to input a positive integer and then output it's factorial. However, the first iteration gets caught up in a continual loop. For example, instead of outputing the factorial of the integer 4, for example, just once and then prompting the user to input another integer I just get the output such as:
4! = 24
4! = 24
4! = 24
4! = 24
4! = 24
...etc. continuing on forever.
Can anyone help? Thank you in advance! Joe
Code:#include <stdio.h> int main() { // Program calculates factorial of user inputted integer long int x,y,result; x=-1; // Controls for negative integer inputted; can only accept positive integer printf("Please enter a positive integer:"); scanf("%d",&x); do { // Calculates the factorial result = 1; for (y=x;y>=1;y--) result = result*y; printf("%d! = %d\n",x,result); } while (x!=0); printf("Negative integer inputted, Please Try Again!!!\n"); return 0; }



LinkBack URL
About LinkBacks


