codescracker


java

Java Program to Print Star Pyramid Patterns



« Previous Program Next Program »


Print Patterns of Numbers and Stars

To print patterns of numbers and stars (*) in Java Programming, you have to use two loops, first is outer loop and the second is inner loop. The outer loop is responsible for rows and the inner loop is responsible for columns.

Java Programming Code to Prints Patterns

Following are the one by one Java Programs to print different-different patterns :

Java Program - Pattern 1

public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j;
        for(i=0; i<5; i++)
        {
            for(j=0; j<=i; j++)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program print patterns

Java Program - Pattern 2

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

When the above Java Program is compile and executed, it will produce the following output:

print pattern in Java Programming

Java Program - Pattern 3

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

When the above Java Program is compile and executed, it will produce the following output:

Java Program print star pattern

Java Program - Pattern 4

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

When the above Java Program is compile and executed, it will produce the following output:

print star pattern in Java Programming

Java Program - Pattern 5

public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j, n=1;
        for(i=0; i<5; i++)
        {
            for(j=0; j<=i; j++)
            {
                System.out.print(n+ " ");
                n++;
            }
            System.out.println();
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output:

looping in Java Programming

Java Program - Pattern 6

public class JavaProgram
{
    public static void main(String args[])
    {
        int i, j, num;
        for(i=0; i<5; i++)
        {
            num=1;
            for(j=0; j<=i; j++)
            {
                System.out.print(num+ " ");
                num++;
            }
            System.out.println();
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program print pattern using looping

Java Program - Pattern 7

Following Java Program ask to the user to enter the number of rows to print the pyramid of stars:

import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int i, space, rows, k=0;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Number of Rows : ");
        rows = scan.nextInt();
        for(i=1; i<=rows; i++)
        {
            for(space=1; space<=(rows-i); space++)
            {
                System.out.print("  ");
            }
            while(k != (2*i-1))
            {
                System.out.print("* ");
                k++;
            }
            k = 0;
            System.out.println();
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program print star pyramid

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test