KnowledgeBoat Logo

Computer Applications

Write a menu driven program to perform the following tasks:

(a) Tribonacci numbers are a sequence of numbers similar to Fibonacci numbers, except that a number is formed by adding the three previous numbers. Write a program to display the first twenty Tribonacci numbers.
For example;
1, 1, 2, 4, 7, 13, ………………….

(b) Write a program to display all Sunny numbers in the range from 1 to n.
[Hint: A number is said to be sunny number if square root of (n+1) is an integer.]
For example,
3, 8, 15, 24, 35,………………….are sunny numbers.

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

Java

Java Iterative Stmts

13 Likes

Answer

import java.util.Scanner;

public class KboatTribNSunNos
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("1. Tribonacci numbers");
        System.out.println("2. Sunny numbers");
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        
        switch (ch) {
            case 1:
                int a = 1, b = 1, c = 2;
                
                System.out.print(a + " " + b + " " + c);    
                
                for (int i = 4; i <= 20; i++) {
                    int term = a + b + c;
                    System.out.print(" " + term);
                    a = b;
                    b = c;
                    c = term;
                }
                break;

            case 2:
                System.out.print("Enter n: ");
                int n = in.nextInt();
                
                double sqRoot, temp;
                
                for (int i = 3; i <= n; i++)  {                   
                    sqRoot = Math.sqrt(i + 1);
                    temp = sqRoot - Math.floor(sqRoot);
                    if (temp == 0)
                        System.out.print(i + " ");                       
                }                     
                break;

            default:
            System.out.println("Incorrect choice");
            break;
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a menu driven program to perform the following tasks: (a) Tribonacci numbers are a sequence of numbers similar to Fibonacci numbers, except that a number is formed by adding the three previous numbers. Write a program to display the first twenty Tribonacci numbers. For example; 1, 1, 2, 4, 7, 13, …………………. (b) Write a program to display all Sunny numbers in the range from 1 to n. [Hint: A number is said to be sunny number if square root of (n+1) is an integer.] For example, 3, 8, 15, 24, 35,………………….are sunny numbers. For an incorrect option, an appropriate error message should be displayed.BlueJ output of Write a menu driven program to perform the following tasks: (a) Tribonacci numbers are a sequence of numbers similar to Fibonacci numbers, except that a number is formed by adding the three previous numbers. Write a program to display the first twenty Tribonacci numbers. For example; 1, 1, 2, 4, 7, 13, …………………. (b) Write a program to display all Sunny numbers in the range from 1 to n. [Hint: A number is said to be sunny number if square root of (n+1) is an integer.] For example, 3, 8, 15, 24, 35,………………….are sunny numbers. For an incorrect option, an appropriate error message should be displayed.

Answered By

6 Likes


Related Questions