C Program to Print Right Triangle of Numbers in Sine Wave Pattern

Write a C program to print right triangle of numbers in sine wave pattern using for loop.

#include <stdio.h>

int main()
{
	int rows;

	printf("Enter Right Traingle of Numbers in Sine Wave Rows = ");
	scanf("%d", &rows);

	printf("Right Traingle of Numbers in Sine Wave format\n");

	for (int i = 1; i <= rows; i++)
	{
		printf("%d ", i);
		int num = i;

		for (int j = 1; j < i; j++)
		{
			if (j % 2 != 0)
			{
				printf("%d ", (num + ((2 * (rows - i + 1)) - 1)));
				num = num + ((2 * (rows - i + 1)) - 1);
			}
			else
			{
				printf("%d ", (num + 2 * (i - j)));
				num = num + 2 * (i - j);
			}
		}
		printf("\n");
	}
}
C Program to Print Right Triangle of Numbers in Sine Wave Pattern

Another way of writing the C program is to display the sine wave pattern of numbers in the right angled triangle form.

#include <stdio.h>

int main()
{
	int rows;

	printf("Enter Right Traingle of Numbers in Sine Wave Rows = ");
	scanf("%d", &rows);

	printf("Right Traingle of Numbers in Sine Wave Pattern\n");

	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			if (j % 2 == 0)
			{
				printf("%d ", 1 + j * rows - (j - 1) * j / 2 + i - j);
			}
			else
			{
				printf("%d ", 1 + j * rows - (j - 1) * j / 2 + rows - i - 1);
			}
		}
		printf("\n");
	}
}
Enter Right Traingle of Numbers in Sine Wave Rows = 13
Right Traingle of Numbers in Sine Wave Pattern
1 
2 25 
3 24 26 
4 23 27 46 
5 22 28 45 47 
6 21 29 44 48 63 
7 20 30 43 49 62 64 
8 19 31 42 50 61 65 76 
9 18 32 41 51 60 66 75 77 
10 17 33 40 52 59 67 74 78 85 
11 16 34 39 53 58 68 73 79 84 86 
12 15 35 38 54 57 69 72 80 83 87 90 
13 14 36 37 55 56 70 71 81 82 88 89 91 

This C example uses a while loop to print the right triangle pattern of numbers in the sine wave format.

#include <stdio.h>

int main()
{
	int rows;

	printf("Enter Right Traingle of Numbers in Sine Wave Rows = ");
	scanf("%d", &rows);

	printf("Right Traingle of Numbers in Sine Wave Pattern\n");
	int num, j, i = 1;

	while (i <= rows)
	{
		printf("%d ", i);
		num = i;
		j = 1;

		while (j < i)
		{
			if (j % 2 != 0)
			{
				printf("%d ", num + ((2 * (rows - i + 1)) - 1));
				num = num + ((2 * (rows - i + 1)) - 1);
			}
			else
			{
				printf("%d ", num + 2 * (i - j));
				num = num + 2 * (i - j);
			}
			j++;
		}
		printf("\n");
		i++;
	}
}
Enter Right Traingle of Numbers in Sine Wave Rows = 16
Right Traingle of Numbers in Sine Wave Pattern
1 
2 31 
3 30 32 
4 29 33 58 
5 28 34 57 59 
6 27 35 56 60 81 
7 26 36 55 61 80 82 
8 25 37 54 62 79 83 100 
9 24 38 53 63 78 84 99 101 
10 23 39 52 64 77 85 98 102 115 
11 22 40 51 65 76 86 97 103 114 116 
12 21 41 50 66 75 87 96 104 113 117 126 
13 20 42 49 67 74 88 95 105 112 118 125 127 
14 19 43 48 68 73 89 94 106 111 119 124 128 133 
15 18 44 47 69 72 90 93 107 110 120 123 129 132 134 
16 17 45 46 70 71 91 92 108 109 121 122 130 131 135 136