C++ Program/Code to find n!, (N Factorial)

Leave a Comment
//This interactive program will enable the user to enter a number 'n' (integer) & returns with the value //of n! (n factorial)

#include<iostream>                //For Input/Output Stream Function
#include<conio.h>                  //For Console Input/Output function like clrscr & getche
using namespace std;             //Using the "standard" namespace of iostream class

void main()
{
clrscr();                                    //For Clearing the Screen
int n,i,s=1;                                //Here variable 's' is initialized with a value of '1', since multiplication
cout<<"’nEnter as n! :";
cin>>n;                                     //User inputs the value of 'n'
for(i=n;i>=1;i--)                       //looping backwards i.e, from 'n' to '1' we will calculate the factorial
{
s=s*i;                                        //multiplying the 's' variable with itself & 'i' generated by for loop
}
cout<<"\nAns="<<s;              //Printing the result 's'
getche();                                   //To exit the program after an user's keystroke
}

0 comments: