0% found this document useful (0 votes)
9 views9 pages

Class 22

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Class 22

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Q) What will be the output of below program?

ex:
class Test
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
if(i%2==0)
{
System.out.print(i+" "); //2 4 6 8 10
}
}
}
}

Q) What will be the output of below program?

ex:
class Test
{
public static void main(String[] args)
{
int cnt=0;

for(int i=1;i<=10;i++)
{
if(i%2==0)
{
cnt++;
}
}

System.out.println(cnt); // 5
}
}

Loop Pattern
============

1)

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

class Test
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
System.out.print(i+" ");
}
//new line
System.out.println();
}
}
}

2)

1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

class Test
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
System.out.print(j+" ");
}
//new line
System.out.println();
}
}
}

3)

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

ex:

class Test
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
System.out.print("* ");
}
//new line
System.out.println();
}
}
}

4)
4 4 4 4
3 3 3 3
2 2 2 2
1 1 1 1

ex:
class Test
{
public static void main(String[] args)
{
for(int i=4;i>=1;i--)
{
for(int j=1;j<=4;j++)
{
System.out.print(i+" ");
}
//new line
System.out.println();
}
}
}

5)
* * * *
* *
* *
* * * *

ex:
class Test
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i==1 || i==4 || j==1 || j==4)
System.out.print("* ");
else
System.out.print(" ");
}
//new line
System.out.println();
}
}
}

6)
* - - -
- * - -
- - * -
- - - *

class Test
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i==j)
System.out.print("* ");
else
System.out.print("- ");
}
//new line
System.out.println();
}
}
}

7)
* - - - *
- * - * -
- - * - -
- * - * -
* - - - *

class Test
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(i==j || i+j==6)
System.out.print("* ");
else
System.out.print("- ");
}
//new line
System.out.println();
}
}
}

Left Side Loop Patterns


=======================

1)

1
2 2
3 3 3
4 4 4 4

class Test
{
public static void main(String[] args)
{
//rows
for(int i=1;i<=4;i++)
{
//cols
for(int j=1;j<=i;j++)
{
System.out.print(i+" ");
}
//new line
System.out.println();
}
}
}

2)

4 4 4 4
3 3 3
2 2
1

class Test
{
public static void main(String[] args)
{
//rows
for(int i=4;i>=1;i--)
{
//cols
for(int j=1;j<=i;j++)
{
System.out.print(i+" ");
}
//new line
System.out.println();
}
}
}

3)
*
* *
* * *
* * * *

class Test
{
public static void main(String[] args)
{
//rows
for(int i=1;i<=4;i++)
{
//cols
for(int j=1;j<=i;j++)
{
System.out.print("* ");
}
//new line
System.out.println();
}
}
}
4)

1
2 3
4 5 6
7 8 9 0

ex:

class Test
{
public static void main(String[] args)
{
int k=1;

//rows
for(int i=1;i<=4;i++)
{
//cols
for(int j=1;j<=i;j++)
{
if(k<=9)
System.out.print(k++ +" ");
else
System.out.print("0 ");
}
//new line
System.out.println();
}
}
}

5)

2
4 6
8 10 12
14 16 18 20

class Test
{
public static void main(String[] args)
{
int k=2;

//rows
for(int i=1;i<=4;i++)
{
//cols
for(int j=1;j<=i;j++)
{
System.out.print(k+" ");
k+=2;
}
//new line
System.out.println();
}
}
}

6)
1
3 5
7 9 11
13 15 17 19

ex:
---

class Test
{
public static void main(String[] args)
{
int k=1;

//rows
for(int i=1;i<=4;i++)
{
//cols
for(int j=1;j<=i;j++)
{
System.out.print(k+" ");
k+=2;
}
//new line
System.out.println();
}
}
}

7)
2
3 5
7 11 13
17 19 23 29

ex:
---
class Test
{
public static void main(String[] args)
{
int n=2;

//rows
for(int i=1;i<=4;i++)
{
//cols
for(int j=1;j<=i;j++)
{
while(true)
{
boolean flag=true;
for(int k=2;k<=n/2;k++)
{
if(n%k==0)
{
flag=false;
break;
}
}
if(flag==true)
{
System.out.print(n+" ");
n++;
break;
}
else
{
n++;
}
}
}
//new line
System.out.println();
}
}
}

8)
1
2 1
1 2 3
4 3 2 1

ex:

class Test
{
public static void main(String[] args)
{
//rows
for(int i=1;i<=4;i++)
{
if(i%2!=0)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+" ");
}
//new line
System.out.println();
}
else
{
for(int j=i;j>=1;j--)
{
System.out.print(j+" ");
}
//new line
System.out.println();
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy