Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.     int num = 0;
  6.     char yn = 'y';
  7.     int i = 1;
  8.     int fac = 1;
  9.     //defining all the vars
  10.     while('y' == yn)
  11.     {
  12.         do{
  13.             printf("Please enter a positive number: ");
  14.             scanf("%d",&num);
  15.             getchar();
  16.             if(num<0)
  17.             {
  18.                 printf("Invalid input!\n");
  19.             }
  20.             //getting a number from user
  21.         }while (num<0);
  22.         //loop will run once to get input from user , and keep running if number is invalid until user enters a valid number(positive number)
  23.         if (0 == num)
  24.         {
  25.             printf("The factorial of 0 is 1");
  26.         }
  27.         //The factorial of 0 is 1
  28.         else
  29.         {
  30.             for(i=1 ; i<num+1 ; i++)
  31.             {
  32.                 fac *= i;
  33.             }
  34.             //calculates the factorial number of the number the user entered
  35.             printf("The factorial of %d is %d\n",num , fac);
  36.         }
  37.         //printing the factorial number of the number the user entered
  38.         printf("Would you like to try again?\n");
  39.         printf("Click 'y' for yes or other key for no: ");
  40.         scanf("%c", &yn);
  41.         getchar();
  42.         //asking the user if he wants to try again, getting input from user
  43.         fac=1;
  44.         //resetting vars
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement