Please help I'm trying to write a program in C using nested loops which has an output of 1-8 (newline) 2-9 (newline) 3-10 (newline) 4-11(newline) 5-12. I wrote a program but I know it has to be a simplier way and I don't think I used nested loops correctly because the first for statement is irrilevant. PLEASE HELP

include <stdio.h>
 
int main()
{
                int k, j, i,l, m, n;
                
      for(i=0; i<1; i++)
                                for(k=1; k<9; k++) printf("%d", k);
                
                                printf("\n\n");
 
                                for(j=2; j<10; j++) printf("%d", j);
 
                                printf("\n\n");
                                
                                for(l=3; l<11; l++) printf("%d", l);
 
                                printf("\n\n");
                                
                                for(m=4; m<12; m++) printf("%d", m);
 
                                printf("\n\n");
                                
                                for(n=5; n<13; n++) printf("%d", n);
 
                
                                return 0;
 
}

First, format your code properly.

What does your book/instructor say about a FOR loop and/or statement? What's it do? What does it execute?

What does the program you wrote do? What does it not do?

The for loop allows one or more statements to be repeated for a specific number of times. It initializes a value, does a conditional test and increments or decrements???

Please help I'm trying to write a program in C using nested loops which has an output of 1-8 (newline) 2-9 (newline) 3-10 (newline) 4-11(newline) 5-12. I wrote a program but I know it has to be a simplier way and I don't think I used nested loops correctly because the first for statement is irrilevant. PLEASE HELP


[

Is the output expected to look like this?:

12345678
23456789
345678910
4567891011
56789101112

I am having trouble understanding your question.

yes the output is expected to look like that.

Here's a simply way to do it with a while loop. Just add more cases if you need to add another line of numbers.

bool Done = false;
 
while(!Done)
{
 cout << i++ << ' ';
 if(i == 8)
 {
  i = 2;
  cout << '\n';
 }
 
 
 cout << i++ << ' ';
 
 if(i == 9)
 {
  i = 3;
  cout << '\n';
 }
    cout << i++ << ' ';
 
 if(i == 10)
 {
  i = 4;
  cout << '\n';
 }
    cout << i++ << ' ';
 
 
 if( i == 11)
  Done = true;
}

yes the output is expected to look like that.

This could be one of many ways of doing it. Take a look at it. Look also at the format for posting code.

#include <stdio.h>

int main(void)
{
    int a = 1;
    int b = 0;
    int c = 8;
    
    for(a = 1; a <= 5; a++)
    {
        for(b = a; b <= c; b++)
        {
            printf("%d", b);
        }
        c++;
        putchar('\n');
    }
    getchar();
    return 0; 
}
commented: Simple n clean program.i'm just starting in 'C' programming and been havin a real tough time figuring out nested loops.....guess this'd be givin me a headstart.Thank you. +0

Thanks I appreciate it, I'm going to study the logic of how you wrote this code, and I will also look at the format for posting. Thanks again

Thanks I appreciate it

You're welcome.

Now if you are pushing for more nested for loops, you could always get one more loop in the nest.

#include <stdio.h>

int main(void)
{
    int a,b,c;
    
    for(a = 0; a < 5; a++)
    {
        for(b = 1; b <= 8; b++)
        {
            for(c = b; c <= 8; c++)
            {
                printf("%d", c);
            }
        putchar('\n');    
        }

    }
    getchar();
    return 0; 
}

Here's a simply way to do it with a while loop.

And how does that poorly formatted program help with

I'm trying to write a program in C using nested loops which ...

:rolleyes:

Simple n clean program.i'm just starting in 'C' programming and been havin a real tough time figuring out nested loops.....guess this'd be givin me a headstart.Thank you.

commented: Don't post in threads that are solved and over 2 years old +0

1
1 2
2 3 4
4 5 6 7
please give me this pattern code in c programming

hey I need to display an output like this o o
o o
o o
o o bt I cant seem to find the exact code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.