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

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

package Shapes3;

import java.util.Scanner;

public class RightTriNumSineWave1 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Right Traingle of Numbers in Sine Wave Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Right Traingle of Numbers in Sine Wave format");
		
		for (int i = 1; i <= rows; i++ ) 
		{
			System.out.print(i + " ");
			int num = i;
			
			for (int j = 1; j < i; j++ ) 
			{
				if(j % 2 != 0)
				{
					System.out.print((num + ((2 * (rows - i + 1)) - 1)) +" ");
					num = num + ((2 * (rows - i + 1)) - 1);
				}
				else
				{
					System.out.print((num + 2 * (i - j)) +" ");
					num = num + 2 * (i - j);
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print Right Triangle of Numbers in Sine Wave Pattern

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

package Shapes3;

import java.util.Scanner;

public class RightTriNumSineWave11 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Right Traingle of Numbers in Sine Wave Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Right Traingle of Numbers in Sine Wave format");
		
		for (int i = 0; i < rows; i++ ) 
		{		
			for (int j = 0; j <= i; j++ ) 
			{
				if(j % 2 == 0)
				{
					System.out.print(1 + j * rows - (j - 1) * j / 2  + i - j +  " ");
				}
				else
				{
					System.out.print(1 + j * rows - (j - 1) * j / 2  + rows - i - 1 +" ");
				}
			}
			System.out.println();
		}
	}
}
Enter Right Traingle of Numbers in Sine Wave Rows = 13
Right Traingle of Numbers in Sine Wave format
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 Java example prints the right triangle pattern of numbers in the sine wave format using while loop.

package Shapes3;

import java.util.Scanner;

public class RightTriNumSineWave2 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);

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