Question #48288

How do I write a pseudo code for a recursive procedure that takes an integer N as a parameter, and prints all integers between N and 10. And how do I do it for an iterative procedure?

Expert's answer

#include <stdlib.h>

#include <iostream>

#include <stdio.h>

#include <cstdlib>

using namespace std;

void recursion(int p)

{

static int i = 10;

if (p == i) return;

if (p > i)

{

--p;

if (p > 10) cout<<p<<" ";

recursion(p);

}

else

{

++p;

if (p < 10) cout<<p<<" ";

recursion(p);

}

}

void iterative(int p)

{

if (p > 10)

{

for (int i = p; i > 10; --i)

if (i < p) { cout<<i<<" ";}

}

else

{

for (int i = p; i < 10; ++i)

if (i > p) { cout<<i<<" ";}

}

}

int main()

{

int n;

cout<<"Enter the N : ";

cin>>n;

cout<<"all integers between "<<n<<" and 10 : "<<endl;

cout<<"recursion : ";

recursion(n); cout<<endl;

cout<<"recursion : ";

iterative(n); cout<<endl;

system("pause");

return 0;

}

#include <iostream>

#include <stdio.h>

#include <cstdlib>

using namespace std;

void recursion(int p)

{

static int i = 10;

if (p == i) return;

if (p > i)

{

--p;

if (p > 10) cout<<p<<" ";

recursion(p);

}

else

{

++p;

if (p < 10) cout<<p<<" ";

recursion(p);

}

}

void iterative(int p)

{

if (p > 10)

{

for (int i = p; i > 10; --i)

if (i < p) { cout<<i<<" ";}

}

else

{

for (int i = p; i < 10; ++i)

if (i > p) { cout<<i<<" ";}

}

}

int main()

{

int n;

cout<<"Enter the N : ";

cin>>n;

cout<<"all integers between "<<n<<" and 10 : "<<endl;

cout<<"recursion : ";

recursion(n); cout<<endl;

cout<<"recursion : ";

iterative(n); cout<<endl;

system("pause");

return 0;

}

``

## Comments

## Leave a comment