This is what i've made so far.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	int b=1;

	for (int i=1; i<=5; i++)
	{
		for (int k=1; k<=i; k++)
		{
		 cout<<b;
		 b++;
		}
		cout<<endl;
    }

	getch();
	return 0;
}

The output is as below

1
23
456
78910
1112131415

The correct one should be:

1
12
123
12
1

Any help would be greatly appreciated.

Recommended Answers

All 4 Replies

Try restarting b at 1 for every iteration of the outer loop. And instead of one loop counting to 5, consider using two loops: one counting up to 3 and one counting down from 2.

thanks.. finally i've made it to display the correct output.

This is how it looks like

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	int b=1, d=1;


	for (int j=1; j<=3; j++)
	{
		for (int k=1; k<=j; k++)
		{
			cout<<b;
			b++;
		}
		cout<<endl;
		b=1;
	}

	for (int c=2; c>=0; c--)
	{
		for (int a=1; a<=c; a++)
		{
			cout<<d;
			d++;
		}
		cout<<endl;
		d=1;
	}

	getch();
	return 0;
	
}
commented: OK..... +0

Yes here is some problems in your code.You have to update your code like.....

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int i,j;
    cout<<"1";
    cout<<endl;
    for(i=1;i<5;i++)
    {
                    int k=1;
                    for(j=1;j<i;j++)
                    {while(k<=i)
                    {cout<<k;
                    k++;
                    }
                    }cout<<endl;
                    }
                    
                    getch();
                    return 0;
                    }

Sorry aloi93 there is no problem in your code.I just did not see your last code.

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.