Jump to content

Nested For Loop [EXAMPLES!]

- - - - -

  • Please log in to reply
45 replies to this topic

#1
ALPHA

ALPHA

    Newbie

  • Members
  • PipPip
  • 17 posts
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:D)
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

[COLOR="SeaGreen"]//Numbers Ladder

//Nested Loops

//By ALPHA

//On 22 Oct 2008

//www.CodeCall.net

//----------------------[/COLOR]

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)


[COLOR="SeaGreen"]//Nested Loops

//star ladder

//www.CodeCall.net

//By ALPHA

//22 Oct 2008

//--------------[/COLOR]

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)

[COLOR="SeaGreen"]//Nested For Loop

//Sorting elements of an Array

//By ALPHA

//23 Oct 2008

//www.CodeCall.net

//------------------------[/COLOR]

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

[COLOR="SeaGreen"]//Two Dimensional Array

//Nested Loops

//By ALPHA

//On 23 Oct 2008

//www.CodeCall.net

//-------------------------------[/COLOR]

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++) 

[COLOR="SeaGreen"]//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 ^_^[/COLOR]

			{

				System.out.print(city[i][j]+"\t");	

			}

	     System.out.println();

		}

	}

}

4.Program to create multiplication table

[COLOR="SeaGreen"]//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

*/

//------------------------------------[/COLOR]

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

Edited by ALPHA, 24 October 2008 - 08:08 AM.

THINK LIKE A PROGRAMMER !


#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

ALPHA said:

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:D)
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

[COLOR="SeaGreen"]//Numbers Ladder

//Nested Loops

//By ALPHA

//On 22 Oct 2008

//www.CodeCall.net

//----------------------[/COLOR]

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.

*
**
***
****
*****
******
*******
********
*********


[COLOR="SeaGreen"]//Nested Loops

//star ladder

//www.CodeCall.net

//By ALPHA

//22 Oct 2008

//--------------[/COLOR]

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)

[COLOR="SeaGreen"]//Nested For Loop

//Sorting elements of an Array

//By ALPHA

//23 Oct 2008

//www.CodeCall.net

//------------------------[/COLOR]

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

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

[COLOR="SeaGreen"]//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

*/

//------------------------------------[/COLOR]

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
Posted Image

#3
ALPHA

ALPHA

    Newbie

  • Members
  • PipPip
  • 17 posts
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 !


#4
Kevin_Silva

Kevin_Silva

    Newbie

  • Members
  • Pip
  • 2 posts
I need the Java code on drawing the letter K and V using for loop. I really need help

#5
chabielita

chabielita

    Newbie

  • Members
  • Pip
  • 1 posts

whoow.. nice codes.. you gave me an idea for our exam.. hihihi..:D

well since this area are for NESTED for LOOP examples.. just wanna share
my simple codes.. :rolleyes:




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

#6
Ranilplus

Ranilplus

    Newbie

  • Members
  • Pip
  • 1 posts
how to
999999999
_88888888
__7777777
___666666
____55555
_____4444
______333
_______22
________1

#7
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
        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();
        }


#8
asma

asma

    Newbie

  • Members
  • Pip
  • 1 posts
how to do this??
1234
123
12
1
please help me....

#9
GMVResources

GMVResources

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Nice loops you guys! =D

#10
AlexIsOneCoolGuy

AlexIsOneCoolGuy

    Newbie

  • Members
  • Pip
  • 1 posts
Nested loops for Objective-C.
#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:
 ------*------
 -----***-----
 ----*****----
 ---*******---
 --*********--
 -***********-
 *************
 -***********-
 --*********--
 ---*******---
 ----*****----
 -----***-----
 ------*------
 
 */


Edited by AlexIsOneCoolGuy, 23 June 2010 - 04:13 PM.
previous comments made the code unreadable, so I deleted them.


#11
gaurav_rana

gaurav_rana

    Newbie

  • Members
  • Pip
  • 1 posts
hi ,
i am having difficulty in writing a program with output

1
22
333
4444
55555

please help me

#12
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
One loop to count rows and the column loop does as many iterations as current row (size) is.
for (i = 1; i <= 5; i++) {
    for (j = 0; j < i; j++) {
        output i;
    }
    output new line;
}

A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users