Help with Nested Loops, C
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;
}
10 Months Ago
Last Updated
Related Article: odd even count from FILE
is a solved C discussion thread by neveragn that has 6 replies, was last updated 1 year ago and has been tagged with the keywords: count, file, odd.
WhYuLoOkIn
Junior Poster in Training
55 posts since Jan 2007
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 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?
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
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???
WhYuLoOkIn
Junior Poster in Training
55 posts since Jan 2007
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0
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.
Aia
Nearly a Posting Maven
2,394 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 220
Skill Endorsements: 14
yes the output is expected to look like that.
WhYuLoOkIn
Junior Poster in Training
55 posts since Jan 2007
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0
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;
}
Aia
Nearly a Posting Maven
2,394 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 220
Skill Endorsements: 14
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
WhYuLoOkIn
Junior Poster in Training
55 posts since Jan 2007
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0
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;
}
Aia
Nearly a Posting Maven
2,394 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 220
Skill Endorsements: 14
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:
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Question Answered as of 6 Years Ago by
Aia,
WaltP
and
mariocatch WhYuLoOkIn
Junior Poster in Training
55 posts since Jan 2007
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0