Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8650271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:46:39+00:00 2026-06-12T13:46:39+00:00

I have been stumped by this problem. I need to create a pattern such

  • 0

I have been stumped by this problem. I need to create a pattern such as:

1
21
221
2221
22221

Using nested for loops. I have something that does (A)

222221
222221
222221
222221
222221

and used to have something that did (B)

/* 1
* 21
* 221
* 2221
* 22221
* 222221
* 2222222
*/

#include <stdio.h>

main()
{
    int n, c, k;

    printf("Enter number of rows\n");
    scanf("%d",&n);

    for ( c = 1 ; c <= n ; c++ )
    {
        printf("1\n");

        for( k = 1 ; k <= c ; k++ )
        printf("2");


    }

    return 0;
}

Any hints would be helpful.

Solution – Thanks to the intelligent people that decided to help.
I appreciate your help!

#include <stdio.h>

main()
{
    int n, c, k;

    printf("Enter number of rows");
    scanf("%d",&n);

    for ( c = 1 ; c <= n ; c++ )
    {


        for( k = 1 ; k < c ; k++ )
        {
            printf("2");
        }

        printf("1\n");
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:46:40+00:00Added an answer on June 12, 2026 at 1:46 pm

    This can be done using nested for loops. Let’s examine the formulae for one line of the output:

    line 1:

    1

    Which can be made using a simple for loop like this:

    for (int i = 0; i < 1; i++)
    {
        putc('1', stdout);
        putc('\n', stdout);
    }
    

    line 2:

    21

    Hmm, this requires change to our structure, as we can’t break the output of iteration #1, but we still need to be able to add the ‘2’ in there… Something like this should work:

    for (int i = 0; i < 2; i++)
    {
        if (i > 0)
           putc('2', stdout);
    
        putc('1', stdout);
        putc('\n', stdout);
    }
    

    line 3:

    221

    Wait, now we need two ‘2’s in there! How can we do this without breaking line’s 2 and three? Well something like this should do it:

     for (int i = 0; i < 3; i++)
     {
         int j = i;
         while (j--)
         {
             putc('2', stdout);
         }
    
         putc('1', stdout);
         putc('\n', stdout);
     } 
    

    Notice that I used a while loop instead of a for loop. It is an exercise to the reader to figure out how to turn that while loop into a for loop.

    Hopefully this helped you to understand the process behind solving similar problems like this in the future – as it is an important programming skill to have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been stumped for 3 hours now on this problem, I need to
I have been stumped all day on this problem. Basically I want to check
I have been very stumped by this one. I have a fairly large (~1500
Hey this might be a simple question, but i have been stumped on it
Ok I'm stumped. I have been trying to use Simpletip to create a tooltip
So, I am kind of stumped. I have been using a generic repository, and
i have been stumped on this for quite a while now. I am trying
I am stumped on this homework problem. I think I have the right answer
I'm stumped by a regex problem. I've been using them for a project and
I've been stumped for bit on this one - I have a string that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.