Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. int main()
  5. {
  6.    
  7.     char keepRunning = 'y';
  8.     //declaring var
  9.  
  10.     do{
  11.            
  12.             int i  = 0;
  13.             int factorial = 1;
  14.             int number = 0;
  15.             //declaring vars so that every time that the functions runs, their value will be the above value
  16.        
  17.             printf("Enter a valid number: ");
  18.             scanf("%d", &number);
  19.             getchar();
  20.             //getting an input from the user
  21.            
  22.             while(0 > number){
  23.                 printf("Enter a valid number: ");
  24.                 scanf("%d", &number);
  25.                 getchar();
  26.             }
  27.             //if the number is not valid, ask the user to put the number again
  28.             if(0 == number){
  29.                 factorial = 1;
  30.             }
  31.             //0! = 1
  32.             else{
  33.                 for(i = 1; i < number + 1; i++){
  34.                     factorial = factorial * i;
  35.                 }
  36.             }
  37.             //from 1 to the number, it multiplies all of number the numbers.
  38.             printf("factorial of %d is %d \n", number, factorial);
  39.             //prints the factorial
  40.             printf("Would you like to try again?\nClick 'y' for yes or other key for no: ");
  41.             scanf("%c", &keepRunning);
  42.             getchar();
  43.             //asks the user if he wants to try again, "type 'y' if you do"
  44.  
  45.             factorial = 1;
  46.            
  47.        
  48.     }while(keepRunning == 'y');
  49.  
  50.     return 0;
  51.    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement