Java program to print different patterns using stars


Program 1

package com.candidjava.star;

import java.util.Scanner;

public class Star1
{
	public static void main(String args[])
	{

		int i, j, t;
		System.out.println("How many row you want ");
		Scanner sc = new Scanner(System.in);
		t = sc.nextInt();
		for (j = 0; j < t; j++)
		{
			for (i = t - 1; i >= j; i--)
			{
				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 Star2
{
	public static void main(String args[])
	{
		int i, j, n;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the row value");
		n = sc.nextInt();
		for (i = 0; i < n; i++)
		{
			for (j = 0; j <= i; j++)
			{
				System.out.print("*");

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

	}

}

Output 2

Enter the row value

5

*

**

***

****

*****


Program 3

package com.candidjava.star;

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

Output 3

*    *

**  **

******




Related Post

Comments

Himansu
         * ** *** *** ***

©candidjava.com