\(\begin{array}{cccc}1&2&3&4\\2&3&4&1\\3&4&2&1\\4&3&2&1\end{array}\)
Write a program that reads a number (number of lines equal to the number of columns) and displays the above pattern.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
void main()
{
int N, i, j;
do{
printf("Enter N: ");
scanf("%d",&N);
}while(N<=0);
for(i=1; i<=N; i++)
{
for(j=i; j<=N; j++)
printf("%4d", j);
for(j=i-1; j>=1; j--)
printf("%4d", j);
printf("\n");
}
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sum of the terms of the harmonic series and stops when the value of the term added is lower than a positive threshold - iterative