Friday, March 31, 2017

To print the pyramid of stars

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

0 comments:

Post a Comment