Results 1 to 7 of 7

Thread: How to write java program to print pyramid of stars?

  1. #1
    Join Date
    Nov 2009
    Posts
    42

    How to write java program to print pyramid of stars?

    Hi,
    I am T.Y.B.Sc.IT student. I recently started learning java language. Yesterday our sir has given us assignment in which there is a program like write java program to print pyramid of stars. I used lot of various method but I didn't get the solution. If you have any idea about it please share with me.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: How to write java program to print pyramid of stars?

    This is simple program to make pyramid.

    Code:
    public class pyramid
    {
    public static void main(String args[])
    {
    for(int x=1;x<10;++)
    {
    for(int y=o;y<x;++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    }}

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to write java program to print pyramid of stars?

    Code:
    public class pyramid
    {
    public static void main(String args[])
    {
    int c=1;
    
    for(int x=1;x<5;++)
    {
    for(int y=x;y<5;++)
    {
    System.out.print(" ");
    }
    for(int t=1;t<=c;t++)
    {
    if(t%2==0)
    System.out.print(" ");
    else
    System.out.print("*");
    c+=2
    }
    }}

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to write java program to print pyramid of stars?

    Code:
    public static final void printStars(final int height) {
    
    
    final int tl = (height * 2) - 1;
    final int tl_2 = tl / 2;
    
    StringBuilder str = new StringBuilder(tl);
    
    
    for (int p = 0; p < tl; ++p) {
    str1.append(' ');
    }
    
    
    for (int i = 0; i <= tl_2; ++i) {
    str1.setCharAt(tl_2 + i, '*');
    str1setCharAt(tl_2 - i, '*');
    System.out.println(str1);
    }
    
    
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to write java program to print pyramid of stars?

    I think this is simplest program.


    Code:
    package pyramid;
     
    class pyramid{
      public void pyramid(){
        
      } 
      public static void main(String[] args){
        for(int p=1; p<10; p++){
          for(int q = 0; q < p; q++){  
            System.out.print("*"); 
          }
          System.out.println();    
        }
      }
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to write java program to print pyramid of stars?

    Code:
    public class PyramidStars {
    public static void main(String[] args) 
    {
    java.util.Scanner scanner = new java.util.Scanner(System.in);
    System.out.print("Enter no. you want : ");
    int S = scanner.nextInt();
    for (int t = 1; t <= S; t++) {
    for (int s = 1; s<=t; s++) {
    System.out.print("*");
    }
    System.out.println();
    }
    }
    }

  7. #7
    Join Date
    Aug 2011
    Posts
    1

    pyraRe: How to write java program to print x shape of stars?

    how to write java program to print x shape of stars?

Similar Threads

  1. How to write program to generate a pascal triangle in java?
    By Aandaleeb in forum Software Development
    Replies: 7
    Last Post: 11-09-2011, 12:37 PM
  2. how to write this pyramid using java?
    By Rainy in forum Software Development
    Replies: 1
    Last Post: 07-09-2011, 02:30 PM
  3. Need help to write this program in java?
    By frkadeel in forum Software Development
    Replies: 1
    Last Post: 01-12-2010, 03:58 PM
  4. How to write java program to find factorial of number?
    By Balamohan in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 10:14 PM
  5. how to write program on palindrome in java?
    By Linoo in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 05:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,613,697.39410 seconds with 17 queries