+ Reply to Thread
Page 1 of 13 12311 ... LastLast
Results 1 to 10 of 126
Like Tree2Likes

Thread: Nested For Loop [EXAMPLES!]

  1. #1
    ALPHA's Avatar
    ALPHA is offline Newbie
    Join Date
    Oct 2008
    Location
    Deep Ocean
    Posts
    17
    Rep Power
    0

    Nested For Loop [EXAMPLES!]

    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

    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();
    	}
    }
    b.
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    (just bring it to the LEFT side just like it is)

    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();	
    	}
    }
    2.A program for sorting an array elements (ascending sort)

    Code:
    //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]);
    	}
    }
    3.Program to create a Two-Dimensional Array

    Code:
    //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();
    		}
    	}
    }
    4.Program to create multiplication table

    Code:
    //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();
    		}
    	}
    }
    That's all I have for nested loops
    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 !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Nested For Loop [EXAMPLES!]

    Quote Originally Posted by ALPHA View Post
    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

    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();
    	}
    }
    b.
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********

    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();	
    	}
    }
    2.A program for sorting an array elements (ascending sort)

    Code:
    //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]);
    	}
    }
    3.Program to create a Two-Dimensional Array

    Code:
    //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++)
    			{
    				System.out.print(city[i][j]+"\t");	
    			}
    	     System.out.println();
    		}
    	}
    }
    4.Program to create multiplication table

    Code:
    //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();
    		}
    	}
    }
    That's all I have for nested loops
    If anyone has any other ideas, PLEASE post it !

    c u
    ALPHA
    Great stuff,however you should post this in the java tutorials

  4. #3
    ALPHA's Avatar
    ALPHA is offline Newbie
    Join Date
    Oct 2008
    Location
    Deep Ocean
    Posts
    17
    Rep Power
    0

    Re: Nested For Loop [EXAMPLES!]

    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 !

  5. #4
    Kevin_Silva is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Re: Nested For Loop [EXAMPLES!]

    I need the Java code on drawing the letter K and V using for loop. I really need help

  6. #5
    chabielita is offline Newbie
    Join Date
    Feb 2010
    Location
    SOMEWHERE OUT THERE :]
    Posts
    1
    Rep Power
    0

    Smile Re: Nested For Loop [EXAMPLES!]

    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...

  7. #6
    Ranilplus is offline Newbie
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Re: Nested For Loop [EXAMPLES!]

    how to
    999999999
    _88888888
    __7777777
    ___666666
    ____55555
    _____4444
    ______333
    _______22
    ________1

  8. #7
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Nested For Loop [EXAMPLES!]

    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();
            }

  9. #8
    asma is offline Newbie
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Re: Nested For Loop [EXAMPLES!]

    how to do this??
    1234
    123
    12
    1
    please help me....

  10. #9
    GMVResources's Avatar
    GMVResources is offline Learning Programmer
    Join Date
    Jun 2010
    Posts
    72
    Rep Power
    0

    Re: Nested For Loop [EXAMPLES!]

    Nice loops you guys! =D

  11. #10
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Re: Nested For Loop [EXAMPLES!]

    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.

+ Reply to Thread
Page 1 of 13 12311 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 19 users browsing this thread. (0 members and 19 guests)

Similar Threads

  1. Nested for Loop help!!!
    By hotdog916 in forum Java Help
    Replies: 3
    Last Post: 09-28-2011, 02:47 AM
  2. nested loop for
    By tanaysaksena in forum Java Help
    Replies: 1
    Last Post: 05-15-2011, 09:52 PM
  3. nested loop
    By za7ef in forum C and C++
    Replies: 10
    Last Post: 04-08-2010, 02:24 AM
  4. Nested loop *Help*
    By nishanjo in forum Java Help
    Replies: 7
    Last Post: 08-03-2009, 06:33 AM
  5. Nested For Loop [EXAMPLES!]
    By ALPHA in forum Java Help
    Replies: 1
    Last Post: 10-23-2008, 11:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts