1 22 111 2222 11111 make this code from for loop

0 votes
asked Nov 28 by Shahab Ali

14 Answers

0 votes
answered Nov 29 by (140 points)

Following code is the solution for

1 22 111 2222 11111

Code in "C Programming Language"

#include <stdio.h>

int main()
{
    int i,j, m=1, n=2;
    
    for(i=1; i<=5; i++)
    {
        for(j=1; j<=i; j++)
        {
            if(i%2 == 1)
                printf("%d", m);
            else
                printf("%d", n);
        }
        printf(" "); /* \n is replaced with space */
    }
    
    return 0;
}

Code in "Python Programming Language"

for i in range(1, 6):
    for j in range(1, i+1):
        if(i%2 == 1):
            print(1, end="")
        else:
            print(2, end="")
    print("", end=" ")

I assume that you are asking for the following output

1

22

111

2222

11111

If yes, then following code is the solution

written in "C Programming Language" :

#include <stdio.h>

int main()
{
    int i,j, m=1, n=2;
    
    for(i=1; i<=5; i++)
    {
        for(j=1; j<=i; j++)
        {
            if(i%2 == 1)
                printf("%d", m);
            else
                printf("%d", n);
        }
        printf("\n");
    }
    
    return 0;
}

Written in "Python Programming Language" :

for i in range(1, 6):
    for j in range(1, i+1):
        if(i%2 == 1):
            print(1, end="")
        else:
            print(2, end="")
    print("\r")

Hope this helps.. 

0 votes
answered Nov 29 by Chaitanya
public static void main(String[] args) {
  
  int even = 2;
  int odd = 1;
  
  for(int i=1;i<=20; i++){
      if(i%2 == 0){
          for(int j=1; j<=i;j++ ){
              System.out.print(even);
          }
         
      }
      else{
          for(int j=1; j<=i;j++ ){
              System.out.print(odd);
          }
      }
  }
 }
0 votes
answered Nov 30 by Wesley Savage
for i in range(5):
    if(i % 2 == 0):
        num = "1"
    else:
        num = "2"
    string = num
    for b in range (i):
        string += num
    print(string)
0 votes
answered Nov 30 by (140 points)
#include <stdio.h>

int main()

{

int a=5,i,j;

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

{

if(i%2!=0)

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

printf("1");

else

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

printf("2");

printf(" ");

}

}
0 votes
answered Nov 30 by Nuthan Kumar
count = 1
for i in range(1,6):
    if((i%2) == 0):
        print('2'*count)
    else:
        print('1'*count)
    count = count + 1
0 votes
answered Nov 30 by anonymous

#include<stdio.h>
int main(void)
{
int n,i,j;
printf("enter the total number of such sequences you want:");
scanf("%d",&n);

    for(j=1;j<=n;j++)
  {
    for(i=1;i<=2*j-1;i++)
        printf("1");
   printf("\t");
    for(i=1;i<=2*j;i++)
        printf("2");
    printf("\t");
  }
 return 0;
}

0 votes
answered Nov 30 by anonymous
n=int(input())
a="1"
b="2"
for i in range(1,n+1):
    if(i%2==0):
        print(b*i)
    else:
        print(a*i)
0 votes
answered Nov 30 by anonymous
#include<iostream>

using namespace std;

int

main ()

{

  int i, n, j;

  cout << "enter number of times that you want to print\n";

  cin >> n;

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

    {

      cout << " ";

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

{

  if (i % 2 == 1)

    {

      cout << "1";

    }

  else

    {

      cout << "2";

    }

}

    }

}
0 votes
answered Nov 30 by anonymous
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin<<n;
for(int i=0;i<n;i++)
{
    if(i%2==0)
    {
     for(int j=0;j<i+1;j++)
     {
         cout<<"1";
     }
     cout<<" ";
    }
    else
    {
        for(int j=0;j<i+1;j++)
     {
         cout<<"2";
     }
          cout<<" ";
    }
}
}
0 votes
answered Nov 30 by anonymous

#include <stdio.h>

int main()

{

    int num,i,j;

    scanf("%d",&num);

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

    {

        if(i%2==1)

        {

            j=1;

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

            printf("%d",j);

        }

        else

        {

            j=2;

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

            printf("%d",j);

        }

        printf(" ");

    }

    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.
...