Solved

java pyramid program

Posted on 2014-09-16
18
476 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
  • 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
Technology Partners: 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 16

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

679 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