Home > Software engineering >  Recursive function in C to print 1 to n to 1
Recursive function in C to print 1 to n to 1

Time:10-23

For example: for n = 5 the result should be: 123454321. I've managed to do so with 2 functions which will get called one after the other: the first for printing 1 to n, and the second for printing n-1 to 1:

void oneToN(int num)
{
    if (num == 1)
        printf("%d",num);
    else
    {
        oneToN(num-1);
        printf("%d",num);
    }
}
void NToOne(int num)
{
    if(num >= 2)
    {
        printf("%d", num - 1);
        NToOne(num - 1);
    }

}

I was wondering if it's possible with only one recursive function

CodePudding user response:

Yes, it is possible. Here is a general plan on how to deal with recursive problems:

  • Look for the trivial case when you do not need a recursive invocation
  • Imagine that you have a function working, and all you need to do is coming up with just one additional step of the function

Here, you would discover a simple pattern: if you have a function that prints 2345432, all you need to do is to print 1s around it. Hence, you need to supply two numbers - the from and the to. When the numbers are the same, you print one copy, and you are done. Otherwise, you print the from, do the recursive call, and print the from again. Done!

CodePudding user response:

123454321 is 1 2345432 1
2345432 is 2 34543 2
34543 is 3 454 3
etc

See the pattern?

The function should print the current number twice, with a recursive call to handle the higher numbers in between.

void f(int n, int i) {
    if (i == n) {
       printf("%d", i);
    } else {
       printf("%d", i);
       f(n, i   1);
       printf("%d", i);
    }
}

This can be simplified by factoring out the leading (or trailing) printf.

void f(int n, int i) {
    printf("%d", i);
    if (i < n) {
       f(n, i   1);
       printf("%d", i);
    }
}

I would still use a second (non-recursive) function in practice.

void _f(int n, int i) {
    printf("%d", i);
    if (i < n) {
       _f(n, i   1);
       printf("%d", i);
    }
}

void f(int n) {
    if (n > 0) {
       _f(n, 1);
    }

    printf("\n");
}

CodePudding user response:

Calling such a function with NToOne(1, 4) should be enough.

#include <stdio.h>

void
NToOne(int num, int max)
{
    printf("%d", num);
    if(num < max)
        NToOne(num   1, max);
    else if (num == max)
        printf("%d", num 1);
    printf("%d", num);
}

void
print_1_to_n_to_1(int n)
{
    NToOne(1, n-1);
}

void
main(void)
{
    print_1_to_n_to_1(5);
}

CodePudding user response:

Here's how I do this by using single recursive function.

#include<iostream>

using namespace std;
//Recursive Function
void func(int n,int i){
    if(i<=n){
       cout<<i;
       func(n,  i);
    }
    if(i<=n)
      cout<<i-1;
 }

int main(){
   int n;
   cin>>n;
   func(n,1);
   return 0;
}

CodePudding user response:

We can easily get that pattern with only one recursive function called 'work' (https://i.stack.imgur.com/yUzJN.jpg)

  • Related