Solved

java pyramid program

Posted on 2014-09-16
18
463 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 74

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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now