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.

Dani AI

Generated

Good insight from (reset the per-line counter) and well done for the two-phase solution. The root cause was the single running counter: it never restarted, so numbers flowed across lines instead of beginning at 1 each line. Two clean options: restart a counter inside the outer loop, or compute how many numbers each line should show and print 1..count. The latter keeps the code compact and general.

#include <iostream>

int main() {
    int rows = 5; // changeable height
    for (int i = 1; i <= rows; ++i) {
        int count = (i <= (rows + 1) / 2) ? i : rows - i + 1;
        for (int n = 1; n <= count; ++n)
            std::cout << n;
        std::cout << '\n';
    }
    return 0;
}

Explanation: for rows = 5 the count sequence is 1,2,3,2,1, which produces the requested output. For even rows decide whether you want the middle duplicated (use rows/2) or another behavior, and adjust the count formula accordingly.

A few practical tips: avoid non-standard <conio.h>/getch() for portability — use standard I/O or std::cin.get() if you need a pause. If readability matters when numbers exceed 9, print a space between numbers (std::cout << n << ' ') or use std::setw to align columns. Also prefer named variables like rows and count for clarity rather than reusing counters across scopes.

Finally, ’s nested-loop idea is workable but the single-loop-with-count approach above is simpler to maintain and easier to generalize.

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.