Hi,
Here is a collection of few examples for NESTED FOR LOOP.
Simple codes in how to use nested loops,
There is only the code, no explanation (not good at explaining anything)
So, just try to understand the program
Hope this will help as much people as possible
Let's Stop talking and just begin
OK
Here we go!
1.Get the following output :
a.
1
12
123
1234
12345
123456
1234567
12345678
123456789
b.Code://Numbers Ladder //Nested Loops //By ALPHA //On 22 Oct 2008 //www.CodeCall.net //---------------------- public class testFor { public static void main(String [] args) { for (int i=1; i<=9; i++) { System.out.println(); for (int j=1; j<=i; j++) { System.out.print(j); } } System.out.println(); } }
*
**
***
****
*****
******
*******
********
*********
(just bring it to the LEFT side just like it is)
2.A program for sorting an array elements (ascending sort)Code://Nested Loops //star ladder //www.CodeCall.net //By ALPHA //22 Oct 2008 //-------------- public class starForTest { public static void main(String [] args) { for (int i=1; i<=9; i++) { System.out.println(); for (int j=9; j>=i; j--) { System.out.print(" "); } for (int k=1; k<=i; k++) { System.out.print("*"); } } System.out.println(); } }
3.Program to create a Two-Dimensional ArrayCode://Nested For Loop //Sorting elements of an Array //By ALPHA //23 Oct 2008 //www.CodeCall.net //------------------------ public class arrSort { public static void main(String args []) { int num[]=new int[] {20,50,10,70,40,80,30,90,60}; int t=0; for (int i=0; i<num.length; i++) { for (int j=i+1; j<num.length; j++) if (num[i]>num[j]) { t=num[i]; num[i]=num[j]; num[j]=t; } } System.out.println("Sorting elements in Ascending Order :\n"); for (int i=0; i<num.length; i++) System.out.println(num[i]); } }
4.Program to create multiplication tableCode://Two Dimensional Array //Nested Loops //By ALPHA //On 23 Oct 2008 //www.CodeCall.net //------------------------------- public class twoDimArray { public static void main(String [] args) { String city[][]={ {"NewYork","Muscat","London"} , {"Cairo","Beijing","CapeTown"} }; /* Note : hope u already know why * i used Double Qoutes( " ) when * initializing the Array elements */ for (int i=0; i<city.length; i++) { for (int j=0; j<city[i].length; j++) //Note:we could use 2 and 3 instead of "city.length" and "city[i].length //'Cause we already know that we have 2 Rows and 3 Columns ^_^ { System.out.print(city[i][j]+"\t"); } System.out.println(); } } }
That's all I have for nested loopsCode://Multiplication Table //Using Nested Loops //By ALPHA //23 October 2008 //www[dot]CodeCall[dot]net //----------------------------------- /*Important Note : this program includes Parts that * are ONLY for the shape of the OUTPUT * it will NOT be an ERROR if you * missed those Parts but, as said it's only for * the shape of the output */ //------------------------------------ public class multiplicationTable { public static void main(String args[]) { System.out.print(" "); for (int i=1; i<=9; i++) System.out.print(" "+i); System.out.println(); System.out.println("-------------------------------------------------------"); for (int i=1; i<=9; i++) { System.out.print(i+"|"); for (int j=1; j<=9; j++) { if (i*j<10) System.out.print(" "+i*j); else System.out.print(" "+i*j); } System.out.println(); } } }
If anyone has any other ideas, PLEASE post it !
c u
ALPHA
Last edited by ALPHA; 10-24-2008 at 09:08 AM.
THINK LIKE A PROGRAMMER !
sorry guys,
i thought it would be better to post it there
anyway, thanks for moving it to the right section
and by the way, nice sig turk4n![]()
THINK LIKE A PROGRAMMER !
I need the Java code on drawing the letter K and V using for loop. I really need help
whoow.. nice codes.. you gave me an idea for our exam.. hihihi..
well since this area are for NESTED for LOOP examples.. just wanna share
my simple codes..
Code:public class ForLoop1{ public static void main(String[]args){ int count=0; for(count=0;count<13;count++) { System.out.print("*"); } int count1=0; for(count1=0;count1<7;count1++) { System.out.println("\t\t"); System.out.println("\t\t*"); } } }
Output:
*************
*
*
*
*
*
*
*
hope this codes helps addens dis collection...![]()
how to
999999999
_88888888
__7777777
___666666
____55555
_____4444
______333
_______22
________1
Code:for(int i = 9; i > 0; i--){ for(int j = i; j < 9; j++){ System.out.print("_"); } for(int j = i; j > 0; j--){ System.out.print(i); } System.out.println(); }
how to do this??
1234
123
12
1
please help me....
Nice loops you guys! =D
Nested loops for Objective-C.
Code:#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int i,j; // loop for top pyramid of the diamond for (i = 6; i > 0; i--) { for (j = i; j > 0; j--) { printf("-"); } for (j = i; j < 6; j++) { printf("*"); } for (j = i; j <= 6; j++) { printf("*"); } for (j = i; j > 0; j--) { printf("-"); } printf("\n"); } // loop for bottom pyramid of the diamond for (i = 6; i >= 0; i--) { for (j = i; j < 6; j++) { printf("-"); } for (j = i; j > 0; j--) { printf("*"); } for (j = i; j >= 0; j--) { printf("*"); } for (j = i; j < 6; j++) { printf("-"); } printf("\n"); } [pool drain]; return 0; } /* Program output: ------*------ -----***----- ----*****---- ---*******--- --*********-- -***********- ************* -***********- --*********-- ---*******--- ----*****---- -----***----- ------*------ */
Last edited by AlexIsOneCoolGuy; 06-23-2010 at 05:13 PM. Reason: previous comments made the code unreadable, so I deleted them.
There are currently 19 users browsing this thread. (0 members and 19 guests)
Bookmarks