Nested For Loop in java?
Nested For Loop means we will write more than one for loop using nested sequence.it has a collection of inner for loop and outer for loop,
The outer for loop will execute once but the inner for loop will continuously be executed.
The outer for loop will execute once but the inner for loop will continuously be executed.
for(init;condition;increment)
{
for(init;condition;increment)
{
System.out.print("statement");
}
System.out.println();
}
WAP to print the following statement?
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
............................................................................................
A a B b C
A a B b
A a B
A a
A
..................................................................................................
A solution to This Program:
class Pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
int asc=65;
for(int j=1;j<=6-i;j++)
{
if(j%2!=0)
System.out.print((char)asc+" ");
else
{
System.out.print((char)(asc+32)+" ");
asc++;
}
}
System.out.println();
}
}
}
............................................................................................................................................
1 2 3 4 5
5 4 3 2
1 2 3
5 4
1
.......................................................................
class Pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=6-i;j++)
{
if(i%2!=0)
System.out.print(j+" ");
else
{
System.out.print((6-j)+" ");
}
}
System.out.println();
}
}
}
.....................................................................................................................................................
ASSIGNMENT:-
1 0 0 1 0
1 0 0 1
1 0 0
1 0
1
.................................................................................
A B C D E
B C D E
C D E
D E
E
.........................................................................................
8
8 8 8
8 8 8 8 8
8 8 8
8
................................................................................................
1 2 3 4 5
ReplyDelete1 2 3 4
1 2 3
1 2
1
............................................................................................
class nestedloop
{
public static void main(String[]arg)
{
for(int i=5; i>=1; i--)
{
for(int j=1; j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
1 2 3 4 5
ReplyDelete5 4 3 2
1 2 3
5 4
1
.......................................................................
class loop
{
public static void main(String[]arg)
{
for(int i=5; i>=1; i--)
{
for(int j=1; j<=i; j++)
{
if(i%2!=0)
System.out.print(j);
else
System.out.print(6-j);
}
System.out.println();
}
}
}
A B C D E
ReplyDeleteB C D E
C D E
D E
E
---------------------------------------------------------
class loop
{
public static void main(String[]arg)
{
int c=0;
for(int i=65; i<=69; i++)
{
for(int j=i; j<=69; j++)
{
System.out.print((char)j);
}
System.out.println();
c++;
for(int k=1; k<=c;k++)
{
System.out.print(" ");
}
}
}
}
public class Main
ReplyDelete{
public static void main(String[] args)
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
ReplyDeletepublic class Main
{
public static void main(String[] args)
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
11
ReplyDelete22
333
444
55555
{
public static void main(String[] args)
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
54321
ReplyDelete4321
321
21
1
public class Main
{
public static void main(String[] args) {
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
55555
ReplyDelete4444
333
22
1
public class Main
{
public static void main(String[] args) {
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
/******************************************************************************
ReplyDeleteOnline Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
public class Main
{
public static void main(String arg[])
{
for(int i=1;i<=5;i++)
{
for(int k=5;k>=i;k--)
{
System.out.print(" ");
}
for(int j=1;j<=2*i-1;j++)
{
System.out.print("*");
}
System.out.println();
}
for(int i=5;i>=1;i--)
{
for(int k=5;k>=i;k--)
{
System.out.print(" ");
}
for(int j=1;j<=2*i-1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
\\mayur yadav||||||||||||||||||||||||||||||||||||||||||||||||||||
/******************************************************************************//MAyur YaDAV
ReplyDeleteOnline Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
public class Hello
{
public static void main(String arg[])
{
for(int i=1;i<=6;i++)
{
for(int k=5;k>=i;k--)
{
System.out.print(" ");
}
for(int j=1;j<=2*i-1;j++)
{
System.out.print("\uD83D\uDC7D");
}
System.out.println();
}
for(int i=5;i>=1;i--)
{
for(int k=5;k>=i;k--)
{
System.out.print(" ");
}
for(int j=1;j<=2*i-1;j++)
{
System.out.print("\uD83D\uDC7D");
}
System.out.println();
}
}
}
54321
ReplyDelete4321
321
21
1
class Nestedloop1
{
public static void main(String[]arg)
{
for(int i=1; i<=5; i++)
{
for(int j=5; j>=i; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
class Star1
ReplyDelete{
public static void main(String[]args)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
for(int i=1;i<=4;i++)
{
for(int j=4;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
-------------------------------------------------------
*
**
***
****
***
**
*
class Star2
ReplyDelete{
public static void main(String[]args)
{
for(int i=1;i<=4;i++)
{
for(int j=3;j>=i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println();
}
for(int i=1;i<=3;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(" ");
}
for(int j=3;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
-----------------------------------------------------------
*
**
***
****
***
**
*
class Diamond
ReplyDelete{
public static void main(String[]args)
{
for(int i=1;i<=4;i++)
{
for(int j=3;j>=i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("@");
}
for(int l=2;l<=i;l++)
{
System.out.print("@");
}
System.out.println();
}
for(int i=1;i<=3;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" ");
}
for(int k=3;k>=i;k--)
{
System.out.print("@");
}
for(int l=2;l>=i;l--)
{
System.out.print("@");
}
System.out.println();
}
}
}
-------------------------------------------------
*
***
*****
***
*
Post a Comment
If you have any doubt in programming or join online classes then you can contact us by comment .