Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- char keepRunning = 'y';
- //declaring var
- do{
- int i = 0;
- int factorial = 1;
- int number = 0;
- //declaring vars so that every time that the functions runs, their value will be the above value
- printf("Enter a valid number: ");
- scanf("%d", &number);
- getchar();
- //getting an input from the user
- while(0 > number){
- printf("Enter a valid number: ");
- scanf("%d", &number);
- getchar();
- }
- //if the number is not valid, ask the user to put the number again
- if(0 == number){
- factorial = 1;
- }
- //0! = 1
- else{
- for(i = 1; i < number + 1; i++){
- factorial = factorial * i;
- }
- }
- //from 1 to the number, it multiplies all of number the numbers.
- printf("factorial of %d is %d \n", number, factorial);
- //prints the factorial
- printf("Would you like to try again?\nClick 'y' for yes or other key for no: ");
- scanf("%c", &keepRunning);
- getchar();
- //asks the user if he wants to try again, "type 'y' if you do"
- factorial = 1;
- }while(keepRunning == 'y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement