Computer Science, asked by sakshikumari0298, 1 month ago

write a program in c++to display the following pattern..
1
12
123
1234
12345

Answers

Answered by anindyaadhikari13
7

Solution:

Using C++, the co‎de goes like -

#include <iostream>

using namespace std;

int main()  {

 for(int i=1;i<=5;i++) {

       for(int j=1;j<=i;j++)

           cout <<j;

       cout<<endl;

   }

}

Logic:

  • There are two loops in this co‎de. Outer loop iterates in the range i = 1 to 5. Inner loop iterates in the range j = 1 to i.
  • Inside the inner loop, the value of j is displayed. So, when i = 1, inner loop displays 1 on the screen. When i = 2, inner loop displays 12 on the screen and so on.

This can also be solved using 1 loop. Here comes the co‎de -

#include <iostream>

using namespace std;

int main()  {

   int i,j,k=0;

   for(i=1;i<=5;i++) {

         k=k*10+i;

         cout<<k<<endl;

 }

}

See the attachments for output.

•••♪

Attachments:
Similar questions