Ben recently joined in Coding club in his college. On the first day, he got an assignment. He needs to display the numbers as shown in the sample output. Help Ben by writing a suitable program for the pattern.
Input Format
Enter the number of rows(N) in a single line.
Constraints
1≤N≤20
Output Format
Pattern as shown in the sample output.
Sample Input 0
5
Sample Output 0
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9
Sample Input 1
7
Sample Output 1
1 2 3 4 5 6 7
24 8
23 9
22 10
21 11
20 12
19 18 17 16 15 14 13
#include <stdio.h>
int main()
{
printf("N = ");
int N;
scanf("%d", &N);
int i;
printf("\n");
for(i = 1; i <= N; i++)
printf("%d ", i);
printf("\n");
int j = 1;
int k = 0;
for(i = N+1; i <= 2*(N - 1); i++)
{
printf("%d ", 4*(N-1)-k);
k++;
for(j = 1; j < N; j++)
{
printf(" ");
}
printf("%d\n", i);
}
if(N > 1)
{
for(i = 3*N - 2; i >= 2*N - 1; i--)
{
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Comments
Leave a comment