Thursday, 23 June 2016

Program to print a Star Pattern Type-2.

Explanation :
If n=4 then output will be
       *
     *  *
   *  *  *
 *  *  *  * .

Code:

import java.util.Scanner;
public class StarPattern2
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0;
        for (i=1; i<=line; i++)
        {
            for (k=t; k<line; k++)
            {
                System.out.printf(" ");
            }
            for (j=0; j< i; j++)
            {
                System.out.printf(" * ");
                t = t + 1;
            }
            System.out.printf("\n");
        }
    }
 }

No comments:

Post a Comment