Java program to print stars in different ways


Program 1

package com.candidjava.star;

import java.util.Scanner;

public class Star7
{

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

		for (i = 1; i <= n; i++)
		{
			for (k = 0; k <= n - i; k++)
			{
				System.out.print(" ");
			}

			for (j = n; j >= k; j--)
			{
				System.out.print("*");
			}

			System.out.println("");
		}
	}
}

Output 1

how many row you want : 

5

     *

    **

   ***

  ****

 *****

Program 2

package com.candidjava.star;

import java.util.Scanner;

public class Star8
{

	public static void main(String args[])
	{
		int n, i, k, j;
		System.out.println("how many row you want : ");

		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		for (i = 1; i <= n; i++)
		{
			for (k = 0; k <= i; k++)
			{
				System.out.print(" ");
			}

			for (j = n; j >= i; j--)
			{
				System.out.print("*");
			}

			System.out.println("");
		}
	}
}

Output 2

how many row you want : 

5

  *****

   ****

    ***

     **

      *


Program 3

package com.candidjava.star;

public class Star9
{

	public static void main(String args[])
	{
		int i, j, k;
		for (i = 1; i <= 5; i++)
		{
			for (j = 1; j <= 6 - i; j++)
			{
				System.out.print("*");
			}
			for (k = 1; k < i; k++)
			{
				System.out.print("  ");
			}
			for (j = 1; j <= 6 - i; j++)
			{
				System.out.print("*");
			}
			System.out.println("");
		}
		for (i = 2; i <= 5; i++)
		{
			for (j = 1; j <= i; j++)
			{
				System.out.print("*");
			}
			for (k = 1; k <= 5 - i; k++)
			{
				System.out.print("  ");
			}
			for (j = 1; j <= i; j++)
			{
				System.out.print("*");
			}
			System.out.println("");
		}

	}

}

Ouput 3

**********

****  ****

***    ***

**      **

*        *

**      **

***    ***

****  ****

**********





Related Post

Comments


©candidjava.com