Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(void)
- {
- int num = 0;
- char yn = 'y';
- int i = 1;
- int fac = 1;
- //defining all the vars
- while('y' == yn)
- {
- do{
- printf("Please enter a positive number: ");
- scanf("%d",&num);
- getchar();
- if(num<0)
- {
- printf("Invalid input!\n");
- }
- //getting a number from user
- }while (num<0);
- //loop will run once to get input from user , and keep running if number is invalid until user enters a valid number(positive number)
- if (0 == num)
- {
- printf("The factorial of 0 is 1");
- }
- //The factorial of 0 is 1
- else
- {
- for(i=1 ; i<num+1 ; i++)
- {
- fac *= i;
- }
- //calculates the factorial number of the number the user entered
- printf("The factorial of %d is %d\n",num , fac);
- }
- //printing the factorial number of the number the user entered
- printf("Would you like to try again?\n");
- printf("Click 'y' for yes or other key for no: ");
- scanf("%c", &yn);
- getchar();
- //asking the user if he wants to try again, getting input from user
- fac=1;
- //resetting vars
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement