Monday, 6 November 2023

Create a package named Series having three different classes to print series: Fibonacci series Cube of numbers Square of numbers Write a java program to generate ‘n’ terms of the above series.

 

Create a package named Series having three different classes to print series:

Fibonacci series

Cube of numbers

Square of numbers

Write a java program to generate ‘n’ terms of the above series.

 

Fibonacci.java

package Series;

public class Fibonacci {

    public static void generateFibonacciSeries(int n) {

        int a = 0, b = 1;

        System.out.print("Fibonacci Series (" + n + " terms): ");

        for (int i = 1; i <= n; i++) {

            System.out.print(a + " ");

            int sum = a + b;

            a = b;

            b = sum;

        }

    }

}

CubeOfNumbers.java

package Series;

 

public class CubeOfNumbers {

    public static void generateCubeSeries(int n) {

        System.out.print("Cube of Numbers (" + n + " terms): ");

        for (int i = 1; i <= n; i++) {

            int cube = i * i * i;

            System.out.print(cube + " ");

        }

    }

}

SquareOfNumbers.java

package Series;

 

public class SquareOfNumbers {

    public static void generateSquareSeries(int n) {

        System.out.print("Square of Numbers (" + n + " terms): ");

        for (int i = 1; i <= n; i++) {

            int square = i * i;

            System.out.print(square + " ");

        }

    }

}

SeriesMain.java

import Series.*;

 

public class SeriesMain {

    public static void main(String[] args) {

        int n = 10; // Number of terms

 

        Fibonacci.generateFibonacciSeries(n);

        System.out.println();

 

        CubeOfNumbers.generateCubeSeries(n);

        System.out.println();

 

        SquareOfNumbers.generateSquareSeries(n);

        System.out.println();

    }

}