ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2012 ICSE Computer Science Paper



Share with a Friend

Solved 2012 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Menu Driven - Fibonacci / Sum of Digits Program - ICSE 2012 Computer Science

Using the switch statement, write a menu-driven program to:

(i) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5, …

The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.

(ii) Find the sum of the digits of an integer that is input.

Sample Input: 15390

Sample Output: Sum of the digits = 18.



For an incorrect choice, an appropriate error message should be displayed.

import java.util.Scanner; class Menu{ public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.println("1. Fibonacci series"); System.out.println("2. Sum of digits"); System.out.print("Enter your choice: "); int choice = Integer.parseInt(in.nextLine()); switch(choice){ case 1: int a = 0; int b = 1; int c = 0; System.out.print(a + " " + b); for(int i = 3; i <= 10; i++){ c = a + b; System.out.print(" " + c); a = b; b = c; } System.out.println(); break; case 2: System.out.print("Enter the number: "); int n = Integer.parseInt(in.nextLine()); int sum = 0; for(int i = n; i != 0; i /= 10) sum += i % 10; System.out.println("Sum of digits: " + sum); break; default: System.out.println("Invalid choice!"); } } }

Output

 
 OUTPUT 1: 
1. Fibonacci series
2. Sum of digits
Enter your choice: 1
0 1 1 2 3 5 8 13 21 34 

 OUTPUT 2: 
1. Fibonacci series
2. Sum of digits
Enter your choice: 2
Enter the number: 12345
Sum of digits: 15