I am trying to implement these patters using for loop....I am having difficulties with them....If someone can help, would be great!!
I attached the pattern to a Image....

[ATTACH]10180[/ATTACH]

I tries this code for the third pattern...I have no Idea how to get them done....can anyone post codes for all of them?

int n=1,y=1,i=1,x=5;

for (i=1;i<=5;i++)
{   cout<<setw(x);
    for(n=1;n<=y;n++)
    { 

        cout<<"5";
    }
    cout<<endl;

    y++;
    x--;
}

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

Trial and error I reckon and nested for loops.

I tries this code for the third pattern...I have no Idea how to get them done....can anyone post codes for all of them?

This statement, to me, sounds like you are trying to get us to do your homework for you. You must show some measure of good faith and post the code that you have already written (not the ones that you can get to work, obviously), so that we are in a better position to correct any mistakes in it. We (at least, I) will not waste time to write full homework assignments, unless it is our own assignments :P ...

commented: Well said! +32

i am going to tell the first one only,you can try the rest by yourself.

think of first figure as four triangles!

1.thats counts and prints in ascending order
2.thats counts and prints in descending order
3.one that prints spaces " " in ascending order
4.this one also prints spaces " " in ascending order

first of all determine the number of rows you want to print.
here we have 5

so this means we will have to make an outer loop that runs 5 times
,then come the nested loops!....

then all you have to do is,check all the conditions.do some dry runs on your copy first.this way you will understand!

you wont be able to do this unless you try it yourslef!
just practice practice and practice!

here is the code,i had a similar assignment ,way back.changed it a bit.but it prints "5" twice,you figure it out yourself!

for(int x=5;x>0;x--)
	{
		for(int b=0;b<x;b++)
		{
			cout << b+1;
		}
		for(int c=5;c>x;c--)
		{
			cout << " ";
		}
		for(int e=4;e>x;e--)
		{
			cout << " ";
		}
		for(int d=x;d>0;d--)
		{
			cout << d;
		}
		cout << endl;
	}

this is just for learning,try to make your own codes!

i am going to tell the first one only,you can try the rest by yourself.

for(int x=5;x>0;x--)
	{
		for(int b=0;b<x;b++)
		{
			cout << b+1;
		}
		for(int c=5;c>x;c--)
		{
			cout << " ";
		}
		for(int e=4;e>x;e--)
		{
			cout << " ";
		}
		for(int d=x;d>0;d--)
		{
			cout << d;
		}
		cout << endl;
	}

this is just for learning,try to make your own codes!

Thank you.....after little bit of effort I got this result,but not exactly what is supposed to print..

int c=1,a=5,d=1;
for(int i=1;i<=5;i++)
{
	for(int x=5;x>=i;x--)
	{
		cout<<c;
		
		if(c>=a)
		{	cout<<setw(d);
			for(int y=5;y>=x;y--)
			{
				cout<<c;
				c--;
			}
		}	
		c++;
	}
	c=1;
	a--;
	d+=2;
	cout<<endl;
}

what should I do?

thats good,just keep on trying!

you will get the results!

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.