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
Solved

java pyramid program

Posted on 2014-09-16
18
474 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
servlet example issue 6 46
Delphi: barcode reading on android platform 1 51
learn programming 8 33
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

809 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