Solved

java pyramid program

Posted on 2014-09-16
18
485 Views
Last Modified: 2014-09-22
Hi ,

I am working on a challenge to print pyramid as below


                @
               @ @
              @ @ @
             @ @ @ @
            @ @ @ @ @

Rows are not static but they are dynamic. I have to pass the  count of how many rows of linees( in this case say 5)in my method. i have to print pyramid as above. Please advise.Any links resources, sample code highly appreciated. thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 3
  • 3
  • +2
18 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40327026
Is this school work?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40327044
Nope
0
 
LVL 7

Author Comment

by:gudii9
ID: 40327047
I checked few links like
http://www.java-made-easy.com/pyramids.html

http://www.codeobsessed.com/javasourcecode.html

But they are not similar to my challenge.

Please advise
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 40327067
public static void main(String[] args) {

     for(int i=0;i<5;i++) {
         for(int j=0;j<5-i;j++) {
             System.out.print(" ");
         }
        for(int k=0;k<=i;k++) {
            System.out.print("@ ");
        }
        System.out.println();  
    }

}

Above program i tried it is getting desired output but not sure how to pass dynamic line number. please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40327079
public class Pyramid {
	public static void main(String[] args) {
		
		Pyramid pyramid=new Pyramid();
		pyramid.display(10,10,10);
	}
	
	public void display(int i, int j, int k){


	     for( i=0;i<5;i++) {
	         for(j=0;j<5-i;j++) {
	             System.out.print(" ");
	         }
	        for(k=0;k<=i;k++) {
	            System.out.print("$ ");
	        }
	        System.out.println();  
	    }

	
	}
}

Open in new window


I tried as above but no matter what values i am passing in
pyramid.display(10,10,10);

output still same as
     $
    $ $
   $ $ $
  $ $ $ $
 $ $ $ $ $


how to fix this issue. Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40327081
Don't you want to change the "5"?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40327088
i want to pass the number of lines of rows like 5,10 dynamically as method argument/parameter which i am not able to. please advise
0
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 40327094
advise using an argument/parameter in place of the "5"
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 150 total points
ID: 40327095
12 years ago, we had a similar challenge here.  Look at
http://www.experts-exchange.com/Programming/Languages/Java/Q_20263076.html   
There were a variety of elegant answers posted there.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40327115
that is working fine,

what is the purpose of outer for loop with i and inner for loops with j and k. please advise
0
 
LVL 27

Expert Comment

by:rrz
ID: 40327138
that is working fine,
What is working fine?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40327158
Didn't you write those loops?  What purpose did you have in mind?
i loops over rows, j and k loop over columns
0
 
LVL 7

Author Comment

by:gudii9
ID: 40328616
I debugged by putting break points in eclipse.

it is building pyramid top to bottom.

I am wondering what is purpose and meaning of below lines of code

for (int j = 0; j < 5 - i; j++) {
                        System.out.print(" ");

and also last line
System.out.println();
0
 
LVL 7

Author Comment

by:gudii9
ID: 40328627
when i commented  below line seeing output as below

 //System.out.print(" ");

@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @

when i commented  below line seeing output as below
//System.out.println();

     @     @ @    @ @ @   @ @ @ @  @ @ @ @ @ @ @ @ @ @ @

Why j is always getting assigned with 1 rather than 0 at the beginning. please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40328647
public static void main(String[] args) {
		int ar = Integer.parseInt(args[0]);
		Pyramid pyramid = new Pyramid();
		pyramid.display(ar);
	}

	public void display(int lineNumberCount) {

Open in new window


I modified my code slightly as above to pass value through command prompt.(in eclipse through run configuration)
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40329028
This isn't perfect, but it isn't any more "Leaning Tower of Pisa" than your original task's output. Try it.

import java.util.*;

class Pyramid{


public static void main(String[] args){

final int ITERATIONS = 25;
char token = '*';
char space = ' ';
char[] cA_tokens = new char[0];
char[] cA_spaces = new char[0];

for (int x =1; x<ITERATIONS+1;x++){
	
	cA_spaces = java.util.Arrays.copyOf(cA_spaces,(ITERATIONS-x)/2);
	Arrays.fill(cA_spaces,space);
	String spacex = String.valueOf(cA_spaces);
	
	cA_tokens = java.util.Arrays.copyOf(cA_tokens,x);
	Arrays.fill(cA_tokens,token);
	String tokenx = String.valueOf(cA_tokens);
	
		System.out.print(spacex +""+ tokenx);
	
	System.out.print('\n');
}

}


}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40331129
public class Pyramid {
	public static void main(String[] args) {
		int ar = Integer.parseInt(args[0]);
		Pyramid pyramid = new Pyramid();
		pyramid.display(ar);
	}

	public void display(int lineNumberCount) {


	     for( i=0;i<ar;i++) {
	         for(j=0;j<5-i;j++) {
	             System.out.print(" ");
	         }
	        for(k=0;k<=i;k++) {
	            System.out.print("$ ");
	        }
	        System.out.println();  
	    }

	
	}
}

Open in new window


i modified my code as above which is working fine.
0
 
LVL 27

Expert Comment

by:rrz
ID: 40331299
i modified my code as above which is working fine.
That is unbelievable.  Please try
public class Pyramid {
	public static void main(String[] args) {
		int ar = Integer.parseInt(args[0]);
		Pyramid pyramid = new Pyramid();
		pyramid.display(ar);
	}
	public void display(int lineNumberCount) {
	     for(int i=0;i<lineNumberCount;i++) {
	         for(int j=0;j<lineNumberCount-i;j++) {
	             System.out.print(" ");
	         }
	        for(int k=0;k<=i;k++) {
	            System.out.print("$ ");
	        }
	        System.out.println();  
	    }
	}
}
         

Open in new window

0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question