Advertisement
Guest User

Question No. 1

a guest
Nov 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int factorial(int);
  7.  
  8. int main() {
  9.     int again = 1;
  10.     do {
  11.    
  12.     int n, fact;
  13.    
  14.         cout << "Enter a positive number to find factorial : ";
  15.         cin >> n;
  16.    
  17.     while (n <= 0) {
  18.         cout << "\nError : You entered a negative number." << endl;
  19.         cout << "\nPlease enter a positive number to find factorial : ";
  20.         cin >> n;
  21.     }
  22.         fact = factorial(n);
  23.        
  24.         cout << "\nNumber" << setw(14) <<"Factorial" << endl;
  25.         cout << n << setw(13) << fact << endl;
  26.        
  27.         cout << "\nDo you want to repeat the program? (1 = Yes, 0 = No)" << endl;
  28.         cin >> again;
  29.    
  30.     }
  31.     while (again == 1);
  32.     return 0;
  33. }
  34.  
  35. int factorial(int f) {
  36.     int fact = 1;
  37.    
  38.     for (int i = 1; i<=f; i++) {
  39.         fact*=i;
  40.     }
  41.     return fact;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement