for n=5
1
a b c
1 2 3 4 5
a b c d e f g
code is below:
#include <stdio.h>
int main()
{
int num;
int k;
char ch;
printf("Enter the number of row\n");
scanf("%d", &num);
for (int row = 1; row <= num; row++)
{
ch='a';
k=1;
for (int column = 1; column <= (num * 2) - 1; column++)
{
if (row % 2 == 0)
{
if (column >= (num + 1) - row && column <= (num - 1) + row)
{
printf("%c",ch);
ch++;
}
else
{
printf(" ");
}
}
else
{
if (column >= (num + 1) - row && column <= (num - 1) + row)
{
printf("%d",k);
k++;
}
else
{
printf(" ");
}
}
}
printf("\n");
}
return 0;
}
Comments
Post a Comment