?
Solved

java pyramid program

Posted on 2014-09-16
18
Medium Priority
?
497 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 1400 total points
ID: 40327094
advise using an argument/parameter in place of the "5"
0
 
LVL 28

Assisted Solution

by:rrz
rrz earned 600 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 28

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 28

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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