Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

C++ Program To Find Factorial Of A Number, Using Recursive Function

Leave a Comment
/*Finding Factorial of a number using Recursion(or Recursive Function)
Recursive Function : Recursive function is a type of function which calls itself in Function Definition Code Segment until a terminating condition reached. In the following program "fact( )" is a recursive function.*/

#include<iostream.h>
#include<conio.h>

int fact(int n)               //Defining a recursive function for n! calculation 
{
int t;
if(n==1)
return (1);
else
t=n*fact(n-1);
return (t);
}
 
void main()
{
clrscr();
int n;
printf("\nEnter the number");
scanf("%d",&n);
printf("\n factorial=%d",
fact(n));
getche();
}
Read More...

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
}

Read More...