Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run:

Enter number of lines: 7

``````......1
.....212
....32123
...4321234
..543212345
.65432123456
7654321234567``````

(except the example given shows spaces in between the #s in the pyramid & did NOT include the "." I'm using those for placeholders - this is supposed to look like a perfect pyramid - all of the 1's are in a vertical column, all the 2's are in a vertical column, etc.)

I understand how to get the shape (my code is below) - I'm just not sure how to get the digits to change. My code gives me:

``````......1
.....222
....33333
...4444444
..555555555
.66666666666
7777777777777``````

if I enter "7" as the # of lines. I've tried playing with an additional nested loop inside my last one, and it creates craziness. Please help! I've been playing with this for about a day now!!!!

This has to be done only with loops, or if-else statements. We haven't gone over arrays or methods yet.
_______________________________________________________________

``````import java.util.Scanner;

public class Exercise04_17 {
//Displaying a Pyramid

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

//Get user input = number of lines to print in a pyramid
System.out.println("Enter a number between 1 and 15: ");
int numLines = s.nextInt();

for (int row = 1; row <= numLines; row++){

//print out n-row # of spaces
for (int col = 1; col <= numLines-row; col++){
System.out.print(" \t");
}
//print out digits = 2*row-1 # of digits printed
for (int dig= 1; dig <= 2*row-1; dig++){
System.out.print(row + "\t");

}

System.out.println();
}
}

}``````

Your second loop is not correct.

``for (int dig= 1; dig <= 2*row-1; dig++) {}``

You may need to break this loop into 2 loops instead. Once is to print from the 'col' value down to 1, and the other print up from 2 up to 'col' value again. :)

I have a question though... Your input asks for number 1~15, how would you think this display work when your input is 10 or greater than 10? ;)

That's where I'm stuck. When I get to the part :

``````//print out digits = 2*row-1 # of digits printed
for (int dig= 1; dig <= 2*row-1; dig++){
System.out.print(row + "\t");``````

I know that I need (2*row-1) # of digits printed, but I don't know how to tell it to print the values of row + row--, + row-2, etc. I tried that < and of course, that didn't work. Should I not be thinking of those integers in terms of the value of "row"? Should I maybe think of them as values of "col" ? I also tried nesting another loop there, really pulling it out of thin air, (GUESSING!) and my pyramid turned into some kind of parallellogram. :(

Ugh, you're right, using input >10 is a mess :( Should I add an additional space inside the " " for those integers? (But wouldn't that require an additional loop, or if-else statement to test if the integers are >10??) Or am I missing something else vital there??

Also, could you tell me if I am right by defining a new integer "dig" in the last loop? We did an example in class, with a pyramid of stars (*), and my professor used the same integer "col" for that loop when he wrote it out on the board - but NetBeans didn't produce the same results for me that way, so I thought I'd better define a new variable...(Professor does crazy things to confuse us ALL the time!)

For the N>=10, I'd assumed you would use hex. :)

Okay, step away from the code for a minute. Think about what you're trying to do, and then see if the code comes clearer.

In short, you want to print n rows, each one composed of the descending-ascending sequence n..1..n.

Okay, so you have one thing, and you want to do it n times, where n is supplied by the user. Okay, what if you just want to print the last line?
I input 7, and I just get

7654321234567

Can you do that? If you can do that, then you can probably also do

65432123456

and
543212345

and so forth.

So write a method that takes an int n and returns the String n..1..n
Should be pretty easy from there.

Maybe I should explain it even more using jon.kiparsky example...

Look at 65432123456 as an example. If your current code, you would print 66666666666 instead. What I was telling you is to break your current loop into 2 loops instead. In other words, rewrite the loop to be 2 loops. One will write 654321 and the other will write 23456. So do you see it?

If not, look at 543212345 again...
loop 1 print: 54321
loop 2 print: 2345

When you concatenate both print (without a new line between), you will get 543212345, correct? :)

This is my fix for your problem, I changed a few things but it still works (I think thats how you want it to work). Check the following and let me know what you think, does it solve your problem or NOT?...
import java.util.Scanner;

public class Exercise04_17 {
//Displaying a Pyramid
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
//Get user input = number of lines to print in a pyramid
System.out.println("Enter a number between 1 and 7: ");
int numLines = s.nextInt()+2;
for (int row = 1; row <= numLines; row++){
int chk=row;
int chk2=2;
for (int col = 1; col <= numLines-row; col++){
System.out.print(" ");
}
for(int i=0;i<(2*row)-1;i++){
if(chk<1){
System.out.print(chk2++);
}
else{
System.out.print(chk--);
}
}
System.out.println();
}
}
}
/*
I limited the input to 7 because after that, the output output produces double digit numbers. Formating for double digit numbers different from single digit. But anyhow, the algorithm applies, you just have to focus on the formatting is you want to extend it.

:aj
*/

commented: Please don't do people's homework for them. +0

Doing someone's homework for them discourages them from doing it themselves - that's a stupid thing to do. Please don't do that.

And why give them such terrible code? You've made the row printing loop far too complicated, considering what it's being asked to do. What's wrong with counting down to zero in one loop, then counting up to n in a second loop? Simple, legible, doesn't require a gratuitous decision each time through - decisions are expensive, you want to avoid them, not put in extra ones.

Jon, please dont be disrespectful...calling some one stupid is not right either. It doesn't matter how its done as long as it works. I dont even want to know where you are coming from, you just too rude and you have no manners.

I didn't call you stupid, I called your action a stupid one. It is. People have to write code to learn - writing someone's homework for them is a stupid thing to do. Doesn't make you stupid, it means you did something stupid.

As for "it doesn't matter how it's done as long as it works" - wrong. You could not be more wrong. Doing it right is always important, and if you're setting yourself up as some sort of example, it's ten times as important. Your code is bad code because it's inefficient, and it's terrible code because it's intended as an example and it's a bad example.

As for my manners, you're right. I'm a terrible person and it keeps me up at night. So as long as I'm not sleeping anyway, I get plenty of time to write code, so I'm not so bad at it. Maybe if you were more rude, you'd write better code and give better advice.

great, empty and unnecessary reply

I'm vote jon.kiparsky and you

WOW, Fellas...I couldn't use that code anyway because it didn't work with what I had to do - and it confused me - I'd rather think through it myself & understand what I'm doing than just copy something I don't get. But I REALLY do appreciate the feedback.

I FINALLY worked through it...I'm sure there is a better way to code it, but it gives me the correct result. Unless you see something HORRIBLY wrong...???

Thank you guys for all of your help & for steering my brain in the right direction :)

``````import java.util.Scanner;

public class Exercise04_17 {
//Displaying a Pyramid

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

//Get user input = number of lines to print in a pyramid
System.out.println("Enter a number between 1 and 15: ");
int numLines = s.nextInt();

//defined this variable outside the loops for later use
int dig;

for (int row = 1; row <= numLines; row++){

//print out n-row # of spaces
for (int col = 1; col <= numLines-row; col++){
System.out.print(" \t");
}
//print out digits (descending)
for (dig = row; (dig-1) >= 0 ; dig--){
//first, print out the value of row
System.out.print(dig + "\t");
}
//print out digits (ascending)
for (int n = (dig + 1); ((n != row) && (n >= 1)); n++){
System.out.print( (n+1) + "\t");
}

//print return
System.out.println();

}
}

}
``````

Looks good - a bit hard to read (please use the code tags next time) but I don't see anything very wrong with it.

I don't think you need the dig variable. Your ascending loop always starts at 1, doesn't it? It breaks out of the loop at `dig-1 >=0` , so it goes down to `dig==0` , meaning `int n= (dig+1)` always means `int n = (0+1)` . Also, your loop condition in the ascending loop is a little wonky. n can't go below 1, since it starts at 1 and you only add to it (okay, it could end up as a negative only through addition - tell me how that would happen, for a bonus two points of meaningless rep. :) ) so the `(n>=1)` clause is never going to be negative. Then the first clause only stops if n precisely equals row. In this iteration, that's always going to happen, because you're incrementing by 1 each time. What happens if something changes and you end up doing something like printing only even numbers or something? If the increment condition becomes `n+=2` , you could end up with an infinite loop that you'd have to debug. Always make your loop condition <= to avoid this sort of problem.

Otherwise, looks good.

Wow...Well, no worries, because I didn't understand that code - I wouldn't use something unless I understood it! I think I have this figured out...It gives me the correct results, at any rate. I really do appreciate the feedback from all of you guys :)

``````import java.util.Scanner;

public class Exercise04_17 {
//Displaying a Pyramid

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

//Get user input = number of lines to print in a pyramid
System.out.println("Enter a number between 1 and 15: ");
int numLines = s.nextInt();

//defined this variable outside the loops for later use
int dig;

for (int row = 1; row <= numLines; row++){

//print out n-row # of spaces
for (int col = 1; col <= numLines-row; col++){
System.out.print(" \t");
}
//print out digits (descending)
for (dig = row; (dig-1) >= 0 ; dig--){
//first, print out the value of row
System.out.print(dig + "\t");
}
//print out digits (ascending)
for (int n = (dig + 1); ((n != row) && (n >= 1)); n++){
System.out.print( (n+1) + "\t");
}

//print return
System.out.println();

}
}

}``````

**Excuse That Last!!! I was making sure I had the "CODE" thing in there, and I did :( I'm new, I'm sorry :( *

``````      1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
``````

1 2 3 4 5 4 3 2 1
please print this i need the simple code.

Abhishek: welcome to the DaniWeb fora.
Allow me, however, to explain something about the rules of this forum, which are upheld to keep it (easier) maintainable:

1. don't hijack a Thread. if you have a question of your own, start your own Thread. DaniWeb doesn't get billed per Thread started, don't worry about it.
2. don't revive dead Threads. This question was posted (and the last response on it was given) 4 years ago. There's no reason you should post in here, especially since the question is not really related.
3. try for yourself. If it is simple code, as you claim it to be, you shouldn't have any problem writing it yourself, or, at the very least, trying to.

To summarize: two lines of the Rules you agreed on when registering on DaniWeb:

Do provide evidence of having done some work yourself if posting questions from school or work assignments
Do not hijack old forum threads by posting a new question as a reply to an old one

We are all volunteers willing to help you out and explain what you don't understand about what goes wrong in your code, but understand that we volunteer our spare time. We have no obligation whatsoever.
Your post summarized is:

"This is what I need for my homework assignment, make it for me."

Not only are you trying to cheat, and won't you learn anything, but this shows very little respect for those who would help you.

``````import java.util.Scanner;

public class FinalTriangle{
public static void main(String[] args){
Scanner input;
int lines;
do{
input = new Scanner(System.in);
System.out.print("Enter number of lines: ");
lines = input.nextInt();
for(int i = 1; i <= lines; i++){
for(int j = lines - 9; j > 0; j--){
if(i > 9 && lines - i < j)//its this if statement that was the hardest in this problem x33 can change the state if an for loop within it and set the new limit as a bool
j = lines - i + 1;
else
System.out.print("   ");
}

for(int j = i; j < 9; j++)
System.out.print("  ");

for(int j = i; j > 0; j--){
if(i == 1)
System.out.println(j);
else
System.out.print(j + " ");
}

for(int j = 2; j <= i; j++){
if(j == i)
System.out.println(j);
else
System.out.print(j + " ");
}
}

}while(lines != 0);

System.out.println("Goodbye");
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.