[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

Assignment 5

The document contains a Java program that implements a menu-driven application for generating various patterns based on user input. It includes methods for calculating factorials and printing patterns using characters and strings. The program allows users to select different patterns to display, such as right-angled triangles and substrings of a string, and continues until the user chooses to exit.

Uploaded by

ishanrsjain10
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)
4 views5 pages

Assignment 5

The document contains a Java program that implements a menu-driven application for generating various patterns based on user input. It includes methods for calculating factorials and printing patterns using characters and strings. The program allows users to select different patterns to display, such as right-angled triangles and substrings of a string, and continues until the user chooses to exit.

Uploaded by

ishanrsjain10
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/ 5

import java.util.

Scanner;

class PatternMenu {

int i, j;

int factorial(int num) {

int result = 1;

for (i = 1; i <= num; i++) {

result = result * i;

return result;

void pattern1(int n, char ch) {

for (i = 1; i <= n; i++) {

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

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

System.out.println();

void pattern2(int n, char ch) {

for (i = n; i >= 1; i--) {

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

System.out.print(ch);

System.out.println();
}

void pattern3(int n, char ch) {

for (i = 1; i <= n; i++) {

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

System.out.print(" ");

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

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

System.out.println();

void pattern4(String str) {

for (i = 1; i <= str.length(); i++) {

System.out.println(str.substring(0, i));

void start() {

Scanner sc = new Scanner(System.in);

int choice = 0, n = 0;

char ch = ' ';

String str = "";

do {

System.out.println("\nMenu:");
System.out.println("1. Pattern 1");

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

System.out.println("3. Pattern 3");

System.out.println("4. Pattern 4");

System.out.println("5. Exit");

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

choice = sc.nextInt();

switch (choice) {

case 1:

System.out.print("Enter value of n: ");

n = sc.nextInt();

System.out.print("Enter character: ");

ch = sc.next().charAt(0);

pattern1(n, ch);

break;

case 2:

System.out.print("Enter value of n: ");

n = sc.nextInt();

System.out.print("Enter character: ");

ch = sc.next().charAt(0);

pattern2(n, ch);

break;

case 3:

System.out.print("Enter value of n: ");

n = sc.nextInt();

System.out.print("Enter character: ");


ch = sc.next().charAt(0);

pattern3(n, ch);

break;

case 4:

System.out.print("Enter string: ");

str = sc.next();

pattern4(str);

break;

case 5:

System.out.println("Exiting...");

break;

default:

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

} while (choice != 5);

sc.close();

void main()

PatternMenu obj = new PatternMenu();

obj.start();

}
Variable Data
Description
Name Type

i int Loop counter for outer loops

j int Loop counter for inner loops

n int User input: number of rows

User input: character used for


ch char
pattern

User input: string used in pattern


str String
4

choice int Menu selection from the user

You might also like