Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how can I print this using for loop

0 votes
asked Sep 24, 2018 by Arjun
x1

x1x2

x1x2x3

x1x2x3x4

4 Answers

0 votes
answered Sep 24, 2018 by pushpesh kumar
#include<stdio.h>

int main()

{

int i,j;

for(i=1;i<=4;i++)

{

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

{

printf("X%d ",i);

}

printf("\n");

}

return 0;

}
commented Sep 24, 2018 by anonymous
print on j not i
0 votes
answered Sep 24, 2018 by Ritik Bansal (140 points)

FOR PYTHON:--

for i in range(1,5):

    for j in range(1,i+1):

        print('x'+str(i),end='')

    print()

commented Sep 24, 2018 by Ritik Bansal (140 points)
if you want extra space add print() after previous print()
commented Sep 24, 2018 by Ashish
Thank you but it’s printing
X1
X2X2
X3X3X3
X4X4X4X4
0 votes
answered Sep 24, 2018 by anonymous
int main()
{
    cout<<"Hello World";
   for (int i=0;i<5;i++){
       for(int j=0;j<i;j++){
           cout<<"x"<<j+1;
       }
      cout<<endl;
   }
    return 0;
}
0 votes
answered Sep 24, 2018 by anonymous
int main()
{
   for (int i=0;i<5;i++){
       for(int j=0;j<i;j++){
           cout<<"x"<<j+1;
       }
      cout<<endl;
   }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...