Computer Applications
Write a program to display the following patterns as per the user's choice.
Pattern 1
I
C
S
E
Pattern 2
I
C
S
E
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 ch = in.nextInt();
String str = "ICSE";
int len = str.length();
switch (ch) {
case 1:
for (int i = 0; i < len; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
System.out.println(str.charAt(i));
}
break;
case 2:
for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
System.out.println(str.charAt(len - 1 - i));
}
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}Output
Related Questions
Write a program to read the number n via the Scanner class and print the Tribonacci series:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81 …and so on.
Hint: The Tribonacci series is a generalisation of the Fibonacci sequence where each term is the sum of the three preceding terms.
Write a program to calculate the value of Pi with the help of the following series:
Pi = (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) …
Hint: Use while loop with 100000 iterations.
Write a menu driven program to display the pattern as per user’s choice.
Pattern 1
ABCDE
ABCD
ABC
AB
APattern 2
B
LL
UUU
EEEEFor an incorrect option, an appropriate error message should be displayed.
Write a program to input a number and check and print whether it is a Pronic number or not. [Pronic number is the number which is the product of two consecutive integers.]
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7