0

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.

Edited by trickx: n/a

Attachments
#include<iostream>
using namespace std;

int main(){

    int x, y, z=1, spaces=5, num, ctr=2;

    for(y=0; y<6; y++){
        for(x=0; x<spaces; x++)
            cout<<"\t";
            spaces--;
        for(num=1; num<ctr; num*=2)
            cout<<num<<"\t";
            ctr*=2;




        cout<<endl;
    }
    return 0;
}
4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by subtracktor
0

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

0

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?

Edited by trickx: n/a

0

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

0

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.

-2

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;
}
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.

Edited by jonsca: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.