# Thread: Need help!

1. ## Need help!

Assignment:

Part 1: Write a program that requires the user to input a non-negative number and then calculates the factorial of that number and prints the result to the screen. Use a while loop to continuously ask for a number (see example output below). If a negative number other than -1 is entered, print and error message and prompt for another input from the user. If -1 is entered, exit the program.
Part 2: Modify your program (in a different file) using switch to print a menu to the screen, asking the user whether they want to calculate the factorial of a non-negative number, or exit. If they choose to calculate the factorial and enter any negative number, print an error message and take them back to the main menu. If they choose an invalid menu option, print an error message and re-print the menu.

I'm having trouble figuring out how to have the computer calculate a number's factorial and then display it. Also after you enter a negative number I don't know how to return back to the while loop so it asks for another number. Any suggestions? Here's what I got so far:

Code:
```#include <stdio.h>

int main() {

int big, num;

num = 5;

while (num >= 0) {

printf("Enter the number.\n");
scanf("%d", &big);
num=big;
printf("%d!\n", big);
}

printf("ERROR - You have just entered a negative number, please enter a non-negative number.\n");

}```

2. Since you should only exit the while loop when the input is -1, you shouldn't write "while (num >= 0)".

Do you know what the factorial function is?

3. I know what a factorial means, such as if I was given 5! that would mean 5*4*3*2*1 but I don't know how to implement that into my program so that the computer knows to do that.

4. Also, Is there a command that tells the program to go back to the beginning of the program? Because if the number entered is a negative number then the program exits the while loop and goes to the message "Error - you have entered a negative nu . . . . ." but then the program stops. I want the program to go back to the while loop. Any suggestions? (To this question and the one above)

5. To go back to the beginning of the loop? Yes. Either the condition must not be met or you use continue.

6. Originally Posted by engstudent363
I know what a factorial means, such as if I was given 5! that would mean 5*4*3*2*1 but I don't know how to implement that into my program so that the computer knows to do that.
But you just said how to do it. Start at 5, multiply down to 1. If you want to do something repetitively, then you need to use a loop structure.

Originally Posted by engstudent363
Also, Is there a command that tells the program to go back to the beginning of the program? Because if the number entered is a negative number then the program exits the while loop and goes to the message "Error - you have entered a negative nu . . . . ." but then the program stops. I want the program to go back to the while loop. Any suggestions? (To this question and the one above)
Read the response again: if you only want to stop on -1, then your while loop must check for -1, not any negative number. (This means your check for invalid (other negative number) input must also be inside the while loop, so that it gets repeated as well.)

Popular pages Recent additions