| | |
Help with Nested Loops, C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
C Syntax (Toggle Plain Text)
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; }
Doing what they say couldn't be done!!!!
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?
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 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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
[
12345678
23456789
345678910
4567891011
56789101112
I am having trouble understanding your question.
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
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.
c++ Syntax (Toggle Plain Text)
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; }
Last edited by mariocatch; Apr 3rd, 2007 at 6:48 pm.
This could be one of many ways of doing it. Take a look at it. Look also at the format for posting code.
C Syntax (Toggle Plain Text)
#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; }
You're welcome.
Now if you are pushing for more nested for loops, you could always get one more loop in the nest.
Now if you are pushing for more nested for loops, you could always get one more loop in the nest.
C Syntax (Toggle Plain Text)
#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; }
And how does that poorly formatted program help with
:rolleyes:
:rolleyes:
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Program Help Using For Nested Loops (C++)
- converting Nested loops into recursion (C++)
- Nested For Loops (C++)
Other Threads in the C Forum
- Previous Thread: Enter just a number from user
- Next Thread: Simple Tic Tac Game (With Turbo C)
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






