JAVA program for triangle number pattern 23

JAVA program for triangle number pattern 23

This program is to print triangle number pattern 23 in JAVA.

1

3    2

4    5    6

10   9    8    7

11    12   13  14    15

If you need a dry run of the program or any other query, then kindly leave a comment in the comment box or mail me, I would be more than happy to help you.

Program

import java.util.*;
 
class np23
{
	public static void main(String args[])
	{
		int i,j,n,k=1;
 		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter the no of lines");
 		n=sc.nextInt();
    		for(i=1;i<=n;i++)
    		{
			if(i%2==0)
			{
	       			for(j=1;j<=i;j++)
        			{
     					System.out.printf("%3d",k);
					k--;
				} 	
	        		System.out.println();
				k=k+i+1;
			}
			else
			{
	       			for(j=1;j<=i;j++)
        			{
     					System.out.printf("%3d",k);
					k++;
				} 	
     				System.out.println();
				k=k+i;
			}	 			
    		}
	}
}

Output

Share Me!