English

Using a switch statement, write a menu driven program to: a. 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 - Computer Applications

Advertisements
Advertisements

Question

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

  1. 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.
  2. find the sum of the digits of an integer that is input by the user.
    Sample Input: 15390
    Sample Output: Sum of the digits = 18

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

Answer in Brief

Solution

import java.util.*;
public class Q18.
public static void main(String arg[]){
int n, r, sum = 0, a = 0, b = 0, c = 1; i
Scanner ob = new Scanner(System.in);
System.out.println("***MENU***");
System.out.printIn("1. First ten terms of the Fibonacci series"); 
System.out.printin("2. Sum of the digits of the number entered");
System.out.printIn("Enter your choice..1 or 2)
switc(ob.nextInt())
{case 1: System.out.printIn("**Fibonacci Series***");
for (int i = 1; i <= 10; i++) // FIRST 10 TERMS
                 {a = b;
                 b = c;
                 c = a + b;
System.out.print(a + ", ");
                 }
                 break;
case 2: System.out.println("***Sum of Digits*** \n Enter the number");
n = ob.nextInt();
do{
r = n % 10;
sum += r;
n = 1 / 10;
} while (n > 0);
System.out.println("The sum of the digits is " + sum); 
break;
default: System.out.printIn("Invalid entry");
}
}}
shaalaa.com
  Is there an error in this question or solution?
Chapter 1.09: Iterative Constructs in Java - EXERCISES [Page 126]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 1.09 Iterative Constructs in Java
EXERCISES | Q VI. 18. | Page 126
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×