ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2020 ICSE Computer Science Paper



Share with a Friend

Solved 2020 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Menu Driven Program - Pattern & Series - ICSE 2020 Computer Science

Question 8
Write a menu-driven program to perform the following operations as per user’s choice:


(i) To print the value of c = a2 + 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.

import java.util.Scanner; class Menu{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("1. a^2 + 2ab"); System.out.println("2. Pattern"); System.out.print("Enter your choice: "); int choice = Integer.parseInt(in.nextLine()); switch(choice){ case 1: double a = 1.0; final double b = 3.0; double c; while(a <= 20.0){ c = a * a + 2 * a * b; System.out.println("c = " + c); a += 2.0; } 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

 
 OUTPUT 1: 
1. a^2 + 2ab
2. Pattern
Enter your choice: 1
c = 7.0
c = 27.0
c = 55.0
c = 91.0
c = 135.0
c = 187.0
c = 247.0
c = 315.0
c = 391.0
c = 475.0 

 OUTPUT 2: 
1. a^2 + 2ab
2. Pattern
Enter your choice: 2
A
AB
ABC
ABCD
ABCDE