Computer Applications
Using switch-case statement write a menu driven program for the following:
To display the pattern given below:
A
B C
D E F
G H I J
K L M N OTo display the sum of the series given below:
1/1! + 3/3! + 5/5! + ….. + n/n!
Java
Java Nested for Loops
17 Likes
Answer
import java.util.Scanner;
public class KboatMenu
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Pattern");
System.out.println("Type 2 for Series");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice) {
case 1:
char ch = 'A';
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
break;
case 2:
System.out.print("Enter n: ");
int n = in.nextInt();
double s = 0;
for (int i = 1; i <= n; i++) {
double f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
double t = i / f;
s += t;
}
System.out.println("Series Sum = " + s);
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output


Answered By
5 Likes
Related Questions
Write a program in Java to display the following pattern:
a
ce
gik
moqsWrite a program in Java to find the sum of the following series:
S = 1 + (3/2!) + (5/3!) + (7/4!) + ……. to n
Write a program in Java to display the following pattern:
AAAAA
BBBB
CCC
DD
EWrite a program in Java to display the following pattern:
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15