Write C++ program to find factorial of any number


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


Reverse a Number using while loop


  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num,i;
  9. long long fact=1;
  10.  
  11. //Reading a number from user
  12. cout<<"Enter any number to calculate factorial:";
  13. cin>>num;
  14.  
  15. fact = 1;
  16. i = 1;
  17.  
  18. //Run loop from 1 to number entered by user
  19. while(i <= num)
  20. {
  21. fact = fact * i;
  22. i++;
  23. }
  24.  
  25. cout<<"Factorial of "<<num <<" is: "<<fact; ;
  26.  
  27. return 0;
  28. }

Reverse a Number using for loop


  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num,i;
  9. long long fact=1;
  10.  
  11. //Reading a number from user
  12. cout<<"Enter any number to calculate factorial:";
  13. cin>>num;
  14.  
  15. fact = 1;
  16. i = 1;
  17.  
  18. //Run loop from 1 to number entered by user
  19. for(i=1; i<=num; i++)
  20. {
  21. fact = fact * i;
  22. }
  23.  
  24. cout<<"Factorial of "<<num <<" is: "<<fact; ;
  25.  
  26. return 0;
  27. }
  28.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts