Saturday, 17 March 2018

Recursion in C++

The function which is called by itself is called recursion.
OR
When function is called within the same function, it is known as recursion.
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int fact(int);
int i,f,num;
clrscr();
cout<<"Enter any number: ";
cin>>num;
f=fact(num);
cout<<"Factorial: "<<f;
getch();
}
int fact(int a)
{
if(a<0)
return(-1);
if(a==0)
return(1);
else
{
return(a*fact(a-1));
}
}
Output:
Enter any number:5
Factorial:120

1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

Global Infrastructure and its components in AWS

You may operate your workloads wherever and however you choose with the AWS Global Infrastructure, and you'll use the same network, cont...