Hello,
I am studying recursion but I didn't get that how do we know which program is made recursively and which is not .I will be thankfull if someone explains me this .
so you mean If a program has a function other than main() It is recursive and a program that has only one int main() function then it is non recursive.
If a program contains at least one function that calls itself, then it is recursive.
If the program does not contain any such functions, then it is not recursive.
A program is never recursive.
A function may be recursive.
Recursion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
void print( int i )
{
if (i == 0) return;
cout << i << endl;
print( i - 1 );
}
int main()
{
print( 10 );
return 0;
}
This program has a function and it calls itself then how is it not recursive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
void print( int i )
{
while (i > 0)
{
cout << i << endl;
i = i - 1;
}
}
int main()
{
print( 10 ); //function calls itself
return 0
#include <iostream>
usingnamespace std;
void print( int i )
{
if (i == 0) return;
cout << i << endl;
print( i - 1 );
}
int main()
{
print( 10 ); // print does not call itself ,main calls print() ,Then how come this is recursive ?
return 0;
}
#include <iostream>
usingnamespace std;
void print( int i )
{
if (i == 0) return;
cout << i << endl;
print( i - 1 );//if print doesn't call itself what is this?
}
int main()
{
print( 10 );
return 0;
}