HulkStoN
HulkStoN

Reputation: 11

Recursive function in C to print 1 to n to 1

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

Upvotes: 1

Views: 690

Answers (5)

GOWRISHANKAR
GOWRISHANKAR

Reputation: 11

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

Upvotes: -2

ikegami
ikegami

Reputation: 386676

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");
}

Upvotes: 1

Md Tauseef Akhtar
Md Tauseef Akhtar

Reputation: 1

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;
}

Upvotes: 0

alinsoar
alinsoar

Reputation: 15813

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);
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

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!

Upvotes: 4

Related Questions