C++ or C program to calculate factorial of number N using Recursion/ Recursive Function.

File name:- factrec.cpp
#include<iostream.h>
#include<conio.h>
//recursive function of factorial
long factorial(long f){
if(f==1) return 1;
return f*factorial(f-1);
}
void main(){
long fact=1,n;
clrscr();
cout<<"Enter a positive integer number"<<endl;
cin>>n;
if(n>0){
fact=factorial(n);
cout<<"Factorial of "<<n<<" = "<<fact<<endl;
}
else 
cout<<"Wrong number inserted"<<endl;
getch();
}

File name:- factrec.c
#include<stdio.h>
#include<conio.h>
//recursive function of factorial
long factorial(long f){
if(f==1) return 1;
return f*factorial(f-1);
}
void main(){
long fact=1,n;
clrscr();
printf("Enter a positive integer number\n");
scanf("%ld",&n);
if(n>0){
fact=factorial(n);
printf("Factorial of %ld = %ld\n",n,fact);
}
else{
printf("Wrong number inserted\n");
}
getch();
}

No comments:

Post a Comment

Priority Queue

Priority queue:-  It is a special type of queue which stores group of elements. Each element has a priority number associated with it. Prior...