Computer Applications
Write a menu driven program to perform the following operations as per user’s choice:
(i) To print the value of c=a²+2ab, where a varies from 1.0 to 20.0 with increment of 2.0 and b=3.0 is a constant.
(ii) To display the following pattern using for loop:
A
AB
ABC
ABCD
ABCDE
Display proper message for an invalid choice.
Java
Java Conditional Stmts
52 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 Value of c");
System.out.println("Type 2 for pattern");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice) {
case 1:
final double b = 3.0;
for (int a = 1; a <= 20; a += 2) {
double c = Math.pow(a, 2) + 2 * a * b;
System.out.println("Value of c when a is "
+ a + " = " + c);
}
break;
case 2:
for (char i = 'A'; i <= 'E'; i++) {
for (char j = 'A'; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
break;
default:
System.out.println("INVALID CHOICE");
}
}
}
Output



Answered By
17 Likes
Related Questions
Which of the following data type cannot be used with switch case construct?
- int
- char
- String
- double
Rewrite the following code using single if statement.
if(code=='g') System.out.println("GREEN"); else if(code=='G') System.out.println("GREEN");
Write a program to input three numbers (positive or negative). If they are unequal then display the greatest number otherwise, display they are equal. The program also displays whether the numbers entered by the user are 'All positive', 'All negative' or 'Mixed numbers'.
Sample Input: 56, -15, 12
Sample Output:
The greatest number is 56
Entered numbers are mixed numbers.Which of the following is not true with regards to a switch statement?
- checks for an equality between the input and the case labels
- supports floating point constants
- break is used to exit from the switch block
- case labels are unique