C Program to Print Zigzag Numbers in Right Triangle

In this article, we will show how to write a C program to print the zigzag numbers in a right angled triangle row and column using for loop, while loop, and functions.

The below example allows the user to enter the total number of rows, and the nested for loop iterates them from start to end. Next, the program will print the zigzag numbers in each right angled triangle row.

#include <stdio.h>

int main()
{
int rows, i, j, m, n = 1;

printf("Enter Rows = ");
scanf("%d",&rows);

for (i = 1 ; i <= rows; i++ )
{
if (i % 2 != 0)
{
for (j = 1 ; j <= i; j++ )
{
printf("%d ", n);
n++;
}
}
else
{
m = n + i - 1;
for (j = 0 ; j < i; j++ )
{
printf("%d ", m);
m--;
n++;
}
}
printf("\n");
}

return 0;
}
Enter Rows = 9
1 
3 2 
4 5 6 
10 9 8 7 
11 12 13 14 15 
21 20 19 18 17 16 
22 23 24 25 26 27 28 
36 35 34 33 32 31 30 29 
37 38 39 40 41 42 43 44 45 

Instead of a C Programming for loop, this program uses a C Programming while loop to iterate the right angled triangle rows and columns and prints the zigzag numbers in every row. 

#include <stdio.h>

int main()
{
int rows, i, j, m, n = 1;

printf("Enter Rows = ");
scanf("%d",&rows);

i = 1 ;
while (i <= rows )
{
if (i % 2 != 0)
{
j = 1 ;
while (j <= i )
{
printf("%d ", n);
n++;
j++;
}
}
else
{
m = n + i - 1;
j = 0 ;
while ( j < i )
{
printf("%d ", m);
m--;
n++;
j++;
}
}
printf("\n");
i++;
}

return 0;
}
Enter Rows = 12
1 
3 2 
4 5 6 
10 9 8 7 
11 12 13 14 15 
21 20 19 18 17 16 
22 23 24 25 26 27 28 
36 35 34 33 32 31 30 29 
37 38 39 40 41 42 43 44 45 
55 54 53 52 51 50 49 48 47 46 
56 57 58 59 60 61 62 63 64 65 66 
78 77 76 75 74 73 72 71 70 69 68 67 

In this C program, we create a RightTriangleZigzagNumbers function that accepts the user entered rows and prints the right angled triangle of zigzag numbers on each row. For more number programs, please refer to the C Number Pattern Programs article.

#include <stdio.h>

void RightTriangleZigzagNumbers(int rows)
{
int i, j, m, n = 1;
for (i = 1 ; i <= rows; i++ )
{
if (i % 2 != 0)
{
for (j = 1 ; j <= i; j++ )
{
printf("%d ", n);
n++;
}
}
else
{
m = n + i - 1;
for (j = 0 ; j < i; j++ )
{
printf("%d ", m);
m--;
n++;
}
}
printf("\n");
}
}

int main()
{
int rows;

printf("Enter Rows = ");
scanf("%d",&rows);

RightTriangleZigzagNumbers(rows);
return 0;
}
C Program to Print Zigzag Numbers in Right Triangle

Also Read