How can I do this program to show the typed number as many times as the value of the number?

I have to write this program and till now this is what I have.
The program need to read a number from the user and then to show number from 1 to n(typed number from user) as many times as the value of the typed number.
For example : If user types 5 the output should be 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5.

Please can someone help?

int main (){
int nr, i;

cout<<"Type a number: ";
cin>>nr;

for(i=1; i<=nr; i++) {
cout<<i;

if(nr>=i) {
cout<<i;
}
}

return 0;
}
you need two loops.
the inner loop goes to i and writes i, the outer is correct already:

for(i=1; i<=nr; i++)
for (int lc = 0; lc < i ; lc++)
cout<<i;


closed account (E0p9LyTq)
PLEASE learn to use code tags, it makes reading your source MUCH easier.

Hint: you can edit your post and add code tags
http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
   unsigned int num;

   std::cout << "Type a number: ";
   std::cin >> num;

   for (unsigned int loop = 1; loop <= num; loop++)
   {
      for (unsigned int loop2 = 0; loop2 < loop; loop2++)
      {
         std::cout << loop << ' ';
      }
   }

   std::cout << '\n';
}

Type a number: 6
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6
Thank you guys. Got it now. I have worked the same way a program which shows a triangle with *. Thanks.
Topic archived. No new replies allowed.