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<iostream>
using namespace std;

int main()
{
	int rows;

	cout << "Enter Right Traingle of Numbers in Sine Wave Rows = ";
	cin >> rows;

	cout << "Right Traingle of Numbers in Sine Wave format\n";

	for (int i = 1; i <= rows; i++)
	{
		cout << i << " ";
		int num = i;

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

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

#include<iostream>
using namespace std;

int main()
{
	int rows;

	cout << "Enter Right Traingle of Numbers in Sine Wave Rows = ";
	cin >> rows;

	cout << "Right Traingle of Numbers in Sine Wave format\n";

	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			if (j % 2 == 0)
			{
				cout << 1 + j * rows - (j - 1) * j / 2 + i - j << " ";
			}
			else
			{
				cout << 1 + j * rows - (j - 1) * j / 2 + rows - i - 1 << " ";
			}
		}
		cout << "\n";
	}
}
Enter Right Traingle of Numbers in Sine Wave Rows = 10
Right Traingle of Numbers in Sine Wave format
1 
2 19 
3 18 20 
4 17 21 34 
5 16 22 33 35 
6 15 23 32 36 45 
7 14 24 31 37 44 46 
8 13 25 30 38 43 47 52 
9 12 26 29 39 42 48 51 53 
10 11 27 28 40 41 49 50 54 55 

This C++ example prints the right angled triangle pattern of numbers in the sine wave format using a while loop.

#include<iostream>
using namespace std;

int main()
{
	int rows, num, i, j;

	cout << "Enter Right Traingle of Numbers in Sine Wave Rows = ";
	cin >> rows;

	cout << "Right Traingle of Numbers in Sine Wave format\n";
	i = 1;

	while (i <= rows)
	{
		cout << i << " ";
		num = i;
		j = 1;

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