hi!..i really need some help..
i need to print a pyramid pattern shown below in c++ language using a nested for loop...
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1

consider that each spaces are separated by tab..
I can only print this with my source code....
1
1 2
1 2 4
1 2 4 8
1 2 4 8 16
1 2 4 8 16 32

i can't figure out the condition on how to print out the remaining numbers shown below..

1
2 1
4 2 1
8 4 2 1
16 8 4 2 1

any help would be very much appreciated...please help me...

thank you for having time reading this post.

note: that is in pyramid pattern, not a right triangle one.

Recommended Answers

All 7 Replies

You have one loop counting up. Add another one that counts down (the reverse of your counting up loop) before printing the newline.

am..kindly give me an example?
what i know is i should divide a number in order to get a counting down value..
is it correct?

am..how would i do that?
kindly give me an example?
it is very much appreciated...

kindly give me an example?

Please don't act that helpless. You've written most of the code already, so you clearly know how to write a loop (unless you stole the code you attached). You have two nested loops right now, add a third that counts back down to num == 1 .

Also note that your loops need braces if they have more than one statement.

I hope this will help you

#include <iostream>
using namespace std;

int main()
{
	int height = 0;
	cin >> height;

	if(height < 1)
	{
		return 1;
	}

	for(int i = 1; i <= height; i++)
	{
		for(int j = 0; j < i; j++)
		{
			cout << (1 << j) << ' ';
		}
		for(int j = i-2; j >= 0; j--)
		{
			cout << (1 << j) << ' ';
		}
		cout << endl;
	}
	
	return 0;
}

I hope this will help you

No, in fact you've hurt his/her chances of learning from this because you've done it for him/her.

To: Jonsca
Sorry about that. When I start to learning C++, I always appreciate if somebody share his/her code with me. After that I try to write all by myself. That help me to learn reading other people`s code. I think that is as much important as writing a code.
Thats just my opinion.
Actually never mind.

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.