Printing two hollow mirrored triangles recursively in C

#include <stdlib.h>
#include <stdio.h>
int main()
{
 int i,j,n;
 char ch;

printf("Enter number of rows: ");
scanf("%d%c",&n,&ch);
printf("Enter the symbol: ");
ch=getchar();

for(i=1;i<=n;i++)
{

    for(j=1;j<=n-i;j++)
    {
      printf(" ");
    }

    if(i==1 || i==n)
        for(j=1;j<=i;j++)
        {
            printf("%c",ch);
        }
    else
    {
        for(j=1;j<=i;j++)
        {  
            if(j==1 || j==i)
                printf("%c",ch);
            else
                printf(" ");
        }
    }
    printf("\n");               
}             

return 0;
}

This code basically prints a triangle made by x's using a For loop. How can I convert this code into a function implementing Recursion? And how can I print a large X composed of smaller X's with a given “width”, n, which is guaranteed to be odd. Example an X of width n = 7.

728x90

0 Answers Printing two hollow mirrored triangles recursively in C