Java program to print stars in different patterns


Program 1

package com.candidjava.star;

public class Star4
{
	public static void main(String[] x)
	{
		int i, j, k, n = 4;
		for (i = n; i > 0; i--)
		{
			for (j = 1; j <= i; j++)
			{
				System.out.print("*");
			}
			for (j = n - i; j >= 1; j--)
			{
				System.out.print("  ");
			}
			for (k = i; k >= 1; k--)
			{
				System.out.print("*");
			}
			System.out.println();
		}

	}

}

Output 1

********

***  ***

**    **

*      *


Program 2

package com.candidjava.star;

import java.util.Scanner;

public class Star5
{

	public static void main(String args[])
	{
		{
			int i, j, k, n;
			System.out.println("how many row you want : ");
			Scanner sc = new Scanner(System.in);
			n = sc.nextInt();
			for (i = 1; i <= n; i++)
			{
				for (j = i; j <= n; j++)
				{
					System.out.print(" ");

				}
				for (k = 1; k < (2 * i); k++)
				{
					System.out.print("*");
				}
				System.out.println("");

			}
		}

	}
}

Output 2

how many row you want : 

5

     *

    ***

   *****

  *******

 *********


Program 3

package com.candidjava.star;

import java.util.Scanner;

public class Star6
{

	public static void main(String args[])
	{
		int n, i, j, k, l, m;
		// l=0;

		Scanner sc = new Scanner(System.in);
		System.out.println("Enter how many rows");
		m = sc.nextInt();
		int no=m;

		for (i = 0; i < no; i++)
		{
			for (j = 0; j < i; j++)
			{
				System.out.print(" ");
			}
			for (k = 0; k < 2 * m - 1; k++)
			{
				System.out.print("*");

			}
			// l=l+1;
			m = m - 1;
			System.out.println("");
		}
	}

}

Output 3

Enter how many rows

5

*********

 *******

  *****

   ***

    *




Related Post

Comments

Vel Kumar
         1 3 5 7 9 11 13 * 2 4 6 8 10 * * * 3 5 7 * * * * * 4 * * * * * 3 5 7 * * * 2 4 6 8 10 * 1 3 5 7 9 11 13

©candidjava.com