[go: up one dir, main page]

0% found this document useful (0 votes)
16 views16 pages

pattern programs Set2

The document contains multiple Java programs that generate various patterns based on user input or predefined logic. It includes menu-driven programs for displaying different patterns, as well as specific pattern generation using loops. Each program is structured with a main method and utilizes nested loops to produce the desired output.

Uploaded by

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

pattern programs Set2

The document contains multiple Java programs that generate various patterns based on user input or predefined logic. It includes menu-driven programs for displaying different patterns, as well as specific pattern generation using loops. Each program is structured with a main method and utilizes nested loops to produce the desired output.

Uploaded by

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

1) Write a menu-driven program to display the pattern as per user's choice:

Pattern 1 Pattern 2

ABCDE B

ABCD LL

ABC UUU

AB EEEE

For an incorrect option, an appropriate error message should be displayed.

Answer

import java.util.Scanner;

public class KboatMenuPattern

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Enter 1 for pattern 1");

System.out.println("Enter 2 for Pattern 2");

System.out.print("Enter your choice: ");

int choice = in.nextInt();

switch (choice) {

case 1:

for (int i = 69; i >= 65; i--) {

for (int j = 65; j <= i; j++) {


System.out.print((char)j);

System.out.println();

break;

case 2:

String word = "BLUE";

int len = word.length();

for(int i = 0; i < len; i++) {

for(int j = 0; j <= i; j++) {

System.out.print(word.charAt(i));

System.out.println();

break;

default:

System.out.println("Incorrect choice");

break;

}
2) Write a program to generate the following output.

*#

*#*

*#*#

*#*#*

Answer

public class Pattern2

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("# ");

else

System.out.print("* ");

System.out.println();

} }}

3)Write a program in Java to display the following patterns.

2 3

4 5 6

7 8 9 10

11 12 13 14 15
Answer

public class Pattern3

public static void main(String args[]) {

int a = 1;

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(a++ + "\t");

System.out.println();

4)Write a program in Java to display the following patterns.


1 * * * *
* 2 * * *
* * 3 * *
* * * 4 *
* * * * 5
Answer
public class Pattern4
{
public static void main(String args[]) {
char ch = '*';
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(ch + " ");
}
System.out.println();
}
}
}
5) Write a program in Java to display the following patterns.

1****

22***

333**

4444*

55555

Answer

public class Pattern5

public static void main(String args[]) {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(i);

for (int k = 1; k <= 5 - i; k++) {

System.out.print('*');

System.out.println();

}
}

6)Write a program in Java to display the following patterns.

****5

***4

**3

*2

Answer

public class Pattern6

public static void main(String args[]) {

char ch = '*';

for (int i = 5; i >= 1; i--) {

for (int j = i-1; j >= 1 ; j--)

System.out.print(ch + " ");

System.out.print(i);

System.out.println();

7) Write a program in Java to display the following patterns.


A B C D E
A B C D
A B C
A B
A
Answer
public class Pattern7
{
public static void main(String args[]) {
for (int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
System.out.print((char)j);
}
System.out.println();
}
}
}
8) Write a program in Java to display the following patterns.

54321

4321

321

21

Answer

public class Pattern8

public static void main(String args[]) {

for (int i = 5; i >= 1; i--) {

for (int j = i; j >= 1; j--)

System.out.print( j + " ");

System.out.println();

9)Write a program in Java to display the following patterns.

JA
JAV

JAVA

Answer

public class Pattern9

public static void main(String args[]) {

String str = "JAVA";

int len = str.length();

for(int i = 0; i < len; i++) {

for(int j = 0; j <= i; j++) {

System.out.print(str.charAt(j) + " ");

System.out.println();

10) Write the programs in Java to display the following patterns:

21

321

4321

54321

public class Pattern10


{

public static void main(String args[]) {

for (int i = 1; i <= 5; i++) {

for (int j = i; j >= 1; j--) {

System.out.print(j + " ");

} System.out.println();

} }}

11) Write two separate programs to generate the following


patterns using iteration (loop) statements:
(a)
*
*#
*#*
*#*#
*#*#*
public class Pattern11
{
public static void main(String args[]) {
for (int i = 1; i <=5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
}
System.out.println();
}}}
(b)
54321
5432
543
54
5
public class Pattern12
{
public static void main(String args[]) {
for (int i = 1; i <=5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
(g)

99999

77777

55555

33333

11111

public class Pattern13

{ public static void main(String args[]) {

for (int i = 9; i >= 1; i -= 2) {

for (int j = 1; j <= 5; j++) {

System.out.print(i + " ");


} System.out.println(); } }}

14) 9
79
579
3579
13579
public class Pattern14
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = i; j <= 9; j += 2) {
System.out.print(j + " ");
}
System.out.println( );
}}}
15)
9
97
975
9753
97531
public class Pattern
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 9; j >= i; j -= 2) {
System.out.print(j + " ");
}
System.out.println();
}}}
16)Wap to print the following pattern
1
23
456
7 8 9 10
11 12 13 14 15
public class Pattern
{
public static void main(String args[]) {
int k = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++)
{
System.out.print(k++ + " ");
}
System.out.println();
}}}

17)Write a program to generate the following output.


@
@ #
@ # @
@ # @ #
@ # @ # @
public class Pattern17
{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("@ ");
}
System.out.println();
}
}
}

18)Write a program in Java to display the following patterns.


A B C D E
A B C D
A B C
A B
A
public class Pattern18
{
public static void main(String args[]) {
for (int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
System.out.print((char)j);
}
System.out.println();
}
}
}

19)Write a program in Java to display the following patterns.


5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
public class Pattern19
{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print( j + " ");
System.out.println();
}

}
}
20)Write a program in Java to display the following patterns.

IC

ICS

ICSE

public class KboatPattern

public static void main(String args[]) {

String str = "ICSE";

int len = str.length();

for(int i = 0; i < len; i++) {

for(int j = 0; j <= i; j++) {

System.out.print(str.charAt(j) + " ");

System.out.println(); } }}

Question 21
Write a program to generate a triangle or an inverted triangle till n
terms based upon the user's choice of triangle to be displayed.
Example 1
Input:
Type 1 for a triangle and type 2 for an inverted triangle
1
Enter the number of terms
5
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Example 2
Input:
Type 1 for a triangle and type 2 for an inverted triangle
2
Enter the number of terms
6
Output:

666666

55555

4444

333

22

import java.util.Scanner;

public class Pattern20

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Type 1 for a triangle");

System.out.println("Type 2 for an inverted triangle");

System.out.print("Enter your choice: ");

int ch = in.nextInt();

System.out.print("Enter the number of terms: ");

int n = in.nextInt();

switch (ch) {

case 1:
for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(i + " ");

System.out.println();

break;

case 2:

for (int i = n; i > 0; i--) {

for (int j = 1; j <= i; j++) {

System.out.print(i + " ");

System.out.println();

break;

default:

System.out.println("Incorrect Choice");

You might also like