Write Chapter 17 5

Write Chapter 17 5
If you want to Find 5th Question of chapter 17

Search this Book

Showing posts with label chapter 17. Show all posts
Showing posts with label chapter 17. Show all posts

Friday, 8 February 2013

Recursion Chapter 17 > Q.11

Q.11

#include <iostream>
using namespace std;

int func(int x);

void main()
{
cout << func(0) << endl;
cout << func(1) << endl;
cout << func(2) << endl;
cout << func(5) << endl;
}

int func(int x)
{
if (x==0)
return 2;
else if (x==1)
return 3;
else
return (func(x-1) + func(x-2));
}


Output:

Recursion: Chapter 17 > Q.10


Q.10:
#include <iostream>
using namespace std;

int test(int x, int y);

int main()
{
cout << test (5,10) << endl;
cout << test (3,9)  << endl;
}

int test(int x, int y)
{
if (x==y)
return x;
else if (x>y)
return (x+y);
else
return test(x+1,y-1);
}


Output:

Recursion Chapter 17 > Q.9

Q.9
#include <iostream>
using namespace std;

void exercise(int x);

int main()
{
while (1)
{
int x;
cout << "x: ";
cin >> x;

exercise(x);
cout << endl << endl;
}
}

void exercise(int x)
{
if (x>0 && x<10)
{
cout << x << " ";
exercise(x+1);
}
}


Output:

Recursion Chapter 17 > Q.8

Q. 8 (c):
#include <iostream>
using namespace std;

void funcRec(int u, char v);

int main()
{
funcRec(5,'A');
}

void funcRec(int u, char v)
{
if (u == 0)
cout << v;
else if (u == 1)
cout << static_cast<char>(static_cast<int>(v)+1);
else
funcRec(u-1,v);
}

Output: 

Recursion Chapter 17 > Q.7

Q7:
#include <iostream>
using namespace std;

int mystery(int number);

int main()
{
while (1)
{
int x;
cout << "x: ";
cin >> x;

cout << mystery(x) << endl << endl;
}
}

int mystery(int number)
{
if (number == 0)
return number;
else
return (number + mystery(number-1));
}

Output: