Solved programming problem

Recursive function to print an array in the following order.

Source code

#include<stdio.h>

void input(int array[],int n, int i) ///input an array element with recursion

{

    if(i==n)

    {

        return array;

    }

    else

    {

        scanf("%d",&array[i]);

        return input(array,n,i+1);

    }

}

void order_print(int array[],int n,int i) ///print the order we want

{

    if(n%2==0 && n/2==i) ///if the size is even number

    {

        return;

    }

    else if(n%2!=0 && n/2+1==i) ///if the size is odd number

    {

        return;

    }

    else

    {

        printf("%d  %d
",array[i],array[n-1-i]);

        order_print(array,n,i+1);

    }

}

int main()

{

    int n;

    int array[100];

    printf("Please Enter Array Size: ");

    scanf("%d",&n);

    printf("Enter Array Element: ");

    input(array,n,0);

    printf("Your Result : 
");

    order_print(array,n,0);

    printf("
");

    return 0;

}

Output

Please Enter Array Size: 5
Enter Array Element: 1 2  3 4 5
Your Result :
1  5
2  4
3  3

Join the discussion

Add your comment