JAVA program for triangle number pattern 17

JAVA program for triangle number pattern 17

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

1
2 6
3 7 10
4 8 11 13
5 9 12 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 np17
{
	public static void main(String args[])
	{
		int i,j,n,diff,value;
 		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter the no of lines");
 		n=sc.nextInt();
    		for(i=1;i<=n;i++)
    		{
			diff=n-1;
			value=i;
	       		for(j=1;j<=i;j++)
        		{
            			System.out.printf("%4d", value);
			        value=value+diff; 
            			diff--;
        		} 
        		System.out.println("");
    		}
	}
}

Output

Share Me!