Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE Java Programming Switch Case 2017 Q6 Solved

Thursday, January 12, 2023

ICSE Java Programming Switch Case 2017 Q6 Solved

ICSE Java Programming Switch Case  2017 Q6  Solved Computer Application



import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int choice;
        double sum = 0;
        System.out.println("Enter 1 to calculate the sum.");
        System.out.println("Enter 2 to print the series.");
        System.out.println("Enter your choice:");
        choice = in.nextInt();
        switch (choice) {
            case 1:
                for (int i = 1; i <= 5; i++) {
                    sum += Math.pow(2, i) * Math.pow(-1, (double) (i + 1));
                }
                sum -= Math.pow(2, 20);
                System.out.println("The sum is: " + sum);
                break;
            case 2:
                int n;
                System.out.println("Enter the number of terms:");
                n = in.nextInt();
                for (int j = 1; j <= n; j++) {
                    for (int k = 1; k <= j; k++)
                        System.out.print("1");
                    System.out.print("  ");
                }
                break;
            default:
                System.out.println("Wrong choice.");
                break;
        }
        in.close();

    }
}

No comments:

Post a Comment