Nested Loop
Nested Loop
b)
1
22
333
4444
55555
Ans:
class Pattern2
{
public static void main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
c)
1
10
101
1010
10101
Ans:
class Pattern3
{
public static void main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print("0");
}
else
{
System.out.print("1");
}
}
System.out.println();
}
}
}
d)
12345
1234
123
12
1
Ans:
class Pattern4
{
public static void main()
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
e)
54321
5432
543
54
5
Ans:
class Pattern5
{
public static void main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 5; j >= i; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
f) Floyd’s Triangle
1
23
456
7 8 9 10
11 12 13 14
Ans:
class Pattern6
{
public static void main()
{
int a = 1;
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(a+ " ");
a = a+1;
}
System.out.println();
}
}
}
g)
1
33
555
7777
Ans:
class Pattern7
{
public static void main()
{
int a = 1;
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(a);
}
a=a+2;
System.out.println();
}
}
}
Q2) Write a program to input a number and print all prime digits present in that
number.
N = 1234567
Ouput:2357
Ans:
import java.util.*;
class PrimeDigits
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter a number");
n = sc.nextInt();
int d, count =0;
while(n != 0)
{
d = n%10;
if(d == 2 || d == 3 || d == 5 || d == 7)
{
System.out.print(d);
}
n = n/10;
}
}
}
12345
2345
345
45
5
Ans:
class Pattern11
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
for(int j = i; j <= 5; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
c)
2
23
234
2345
23456
Ans:
class Pattern12
{
public static void main(String args[])
{
for(int i = 2;i<=6;i++)
{
for(int j = 2; j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output Questions:
1) What will be the output of the following code?
Output:
1*1
1*2
3*1
3*2
5*1
5*2
H.W:
Q1) Write a program to generate the below patterns:
a)
55555
4444
333
22
1
Ans:
class Pattern13
{
public static void main(String args[])
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
b)
1234
1234
1234
1234
Ans:
class Pattern14
{
public static void main(String args[])
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
c)
1212
1212
1212
1212
Ans:
class Pattern15
{
public static void main(String args[])
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 4; j++)
{
if(j%2 == 0)
{
System.out.print("2");
}
else
{
System.out.print("1");
}
}
System.out.println();
}
}
}
d)
1****
*2***
**3**
***4*
****5
i → 1 to 5
j → 1 to 5
Ans:
class Pattern16
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 5; j++)
{
if(i==j)
{
System.out.print(i);
}
else
{
System.out.print("*");
}
}
System.out.println();
}
}
}
e)
A
BC
DEF
GHIJ
Ans:
class Pattern17
{
public static void main(String args[])
{
char chr = 'A';
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(chr);
chr++;
}
System.out.println();
}
}
}
Q2) Using switch case statement, write a menu driven program as per user’s choice.
(a)
(b)
A 11111
AB 2222
ABC 333
ABCD 44
ABCDE 5
Ans:
import java.util.*;
class MenuDriven1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char ch;
System.out.println("Input a. for character triangle and b. for number triangle");
ch = sc.next().charAt(0);
switch(ch)
{
case 'a':
for(char i = 'A'; i <= 'E'; i++)
{
for(char j = 'A'; j <= i; j++)
{
System.out.print(j);
}
System.out.println();
}
break;
case 'b':
for(int i = 1; i <= 5; i++)
{
for(int j = 5; j >= i; j--)
{
System.out.print(i);
}
System.out.println();
}
break;
default:
System.out.println("Invalid choice");
}
}
}
Nested while loop: A while inside another while loop is called as nested while loop.
Syntax:
Initialisation
while(condition)
{
Initialisation of inner loop
while(condition)
{
Body of inner loop
Updation
}
Body of outer loop
Updation
}
Eg:
Output:
int a = 1;
while(a < = 4) @@@
{ @@@
int b = 1;
while(b < = 3) @@@
{ @@@
System.out.print(“@ ”);
b++;
}
System.out.println();
a++;
}
Nested do-while loop: A do – while loop inside another do – while loop is called as
nested do – while loop.
Syntax:
initialization
do
{
Initialization of inner loop
do
{
Body of inner loop
Updation;
}
while(condition);
body of outer loop;
updation;
}
while(condition);
eg:
int a = 1;
Output:
do
{ @@@
int b = 1; @@@
do
{ @@@
System.out.print(“@ ”); @@@
b++;
}
while(b <= 3);
a++;
System.out.println();
}
while(a <= 4);
Labelled continue statement: When labelled continue statement is executed from inner
loop then it skips the iteration of inner loop and moves for next iteration of outer loop.
Eg:
outer:
for(int a =1 ; a <= 3; a++) Output:
{
1
for(int b = 1; b <= 3; b++)
{ 1
if(b == 2) 1
continue outer;
System.out.println(b);
}
}
Here when continue outer is executed it goes for next iteration of outer loop, skipping
the execution of inner loop.