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

Java Pattern Display Program

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)
56 views16 pages

Java Pattern Display Program

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

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 [Link];

public class KboatMenuPattern

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter 1 for pattern 1");

[Link]("Enter 2 for Pattern 2");

[Link]("Enter your choice: ");

int choice = [Link]();

switch (choice) {

case 1:

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

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


[Link]((char)j);

[Link]();

break;

case 2:

String word = "BLUE";

int len = [Link]();

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

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

[Link]([Link](i));

[Link]();

break;

default:

[Link]("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)

[Link]("# ");

else

[Link]("* ");

[Link]();

} }}

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

[Link](a++ + "\t");

[Link]();

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)
[Link](i + " ");
else
[Link](ch + " ");
}
[Link]();
}
}
}
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++) {

[Link](i);

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

[Link]('*');

[Link]();

}
}

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

[Link](ch + " ");

[Link](i);

[Link]();

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++) {
[Link]((char)j);
}
[Link]();
}
}
}
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--)

[Link]( j + " ");

[Link]();

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 = [Link]();

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

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

[Link]([Link](j) + " ");

[Link]();

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

[Link](j + " ");

} [Link]();

} }}

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)
[Link]("# ");
else
[Link]("* ");
}
[Link]();
}}}
(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--) {
[Link](j + " ");
}
[Link]();
}
}
}
(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++) {

[Link](i + " ");


} [Link](); } }}

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) {
[Link](j + " ");
}
[Link]( );
}}}
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) {
[Link](j + " ");
}
[Link]();
}}}
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++)
{
[Link](k++ + " ");
}
[Link]();
}}}

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)
[Link]("# ");
else
[Link]("@ ");
}
[Link]();
}
}
}

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++) {
[Link]((char)j);
}
[Link]();
}
}
}

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--)
[Link]( j + " ");
[Link]();
}

}
}
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 = [Link]();

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

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

[Link]([Link](j) + " ");

[Link](); } }}

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 [Link];

public class Pattern20

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Type 1 for a triangle");

[Link]("Type 2 for an inverted triangle");

[Link]("Enter your choice: ");

int ch = [Link]();

[Link]("Enter the number of terms: ");

int n = [Link]();

switch (ch) {

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

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

[Link](i + " ");

[Link]();

break;

case 2:

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

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

[Link](i + " ");

[Link]();

break;

default:

[Link]("Incorrect Choice");

Common questions

Powered by AI

In Pattern14, the logic utilizes nested loops where the outer loop iterates downward over odd numbers from 9 to 1. For each odd number, the inner loop iterates upwards through every subsequent odd number until 9, creating increasing sequences within each row. This reveals a sophisticated use of iteration sequence control, demonstrating how careful manipulation of loop bounds and step sizes can create dynamically expanding or contracting sequences, showcasing how loop nesting can directly affect output patterns such as pyramid-like or stair-step structures .

The Pattern9 Java program uses nested loops to generate a pattern based on the word "JAVA". The outer loop iterates over each letter of the string, while the inner loop writes the letters from the start of the string up to the current position of the outer loop, creating a stepwise build-up of the word. This builds a pattern that appears as a staircase effect with increasing characters displayed on each row .

The Pattern3 program generates a pattern where numbers increment from 1 upwards across multiple lines. It displays numbers in a triangular format where each row starts with 1 and continues incrementing consecutively. Specifically, the first row has one number, the second row two numbers, continuing up to the fifth row with five numbers, filling the pattern with integers from 1 to 15 .

The KboatMenuPattern program handles incorrect user inputs by implementing a default case in a switch statement that outputs "Incorrect choice" if the user's input does not match any of the predefined cases (case 1 and case 2). This implies that the program is designed to guide users toward making correct input choices and provides feedback when an input is invalid, suggesting a focus on user experience by ensuring that the user is informed of mistakes and that the program remains functional with unexpected inputs .

The Pattern20 program utilizes user input to select between generating a triangle and an inverted triangle pattern. It takes an integer input to determine the number of terms and a choice input to decide the pattern type. This flexible design allows for varied output, serving multiple user needs within a single program. Such flexibility is advantageous in educational and testing environments where diverse pattern displays can help visualize different structural concepts of loops and conditionals. It also showcases how user-driven input can dynamically control program logic for variable outputs .

The Pattern17 program differentiates between '@' and '#' characters via a conditional check within its nested loops: it uses a modulus operation to alternate between printing '@' and '#'. Specifically, if the index j is even, it prints '#'; otherwise, it prints '@'. This alternation creates a varied pattern effect in each row that differentiates between even and odd positions, signifying an intentional design choice to create visual diversity and complexity within a simple pattern format, adding a layer of sophistication to the otherwise linear output .

The nested loop structure in Pattern19 is significant as it achieves a descending pattern by using an outer loop that decreases from 5 to 1. For each iteration of the outer loop, the inner loop decreases from the current outer loop value down to 1, printing the decreasing numbers. This structure effectively constructs a descending numeric pattern across multiple lines, illustrating the importance of decrementing logic within loops to control the flow and appearance of numeric sequences in pattern outputs .

The program generating the 'ICSE' string pattern differentiates itself by using a fixed string "ICSE" and iteratively printing its characters in an incremental sequence across each row. Each row displays one more character than the previous, starting with 'I', then 'I C', and so on, eventually displaying the entire string 'ICSE'. This approach differs from others that dynamically vary output based on loops that modify or increment character indices directly, rather than using a predefined constant string value .

The Pattern13 program uses a logical strategy where the outer loop iterates over odd numbers decrementing by 2, starting from 9 down to 1. For each value of i, the inner loop prints the current value of i, ensuring that the same number is printed five times in each row. This repetition within the inner loop guarantees consistent row lengths throughout the pattern, providing a uniform appearance across different rows by keeping the number of elements in each row constant .

The Pattern6 program generates a pattern where both asterisks and numbers decrement. Each row consists of a decreasing number of asterisks followed by a number which decreases with each successive line. This is achieved by using an outer loop that decreases from 5 to 1 and an inner loop that outputs (i-1) asterisks before printing the current value of i, effectively creating a mirrored right-aligned triangular pattern with decreasing numbering .

You might also like