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.
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
Related Questions
A triangle is said to be an 'Equable Triangle', if the area of the triangle is equal to its perimeter. Write a program to enter three sides of a triangle. Check and print whether the triangle is equable or not.
For example, a right angled triangle with sides 5, 12 and 13 has its area and perimeter both equal to 30.The statement that brings the control back to the calling method is:
- break
- System.exit(0)
- continue
- return