Fibonacci Series using Inheritence in Java

[2908 views]


What is Fibonacci Series?

A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding or previous numbers. For example, Fibonacci for 6 is 1, 1, 2, 3, 5, 8, etc.

Fibonacci Series using Inheritence in Java

Super Class

class fib { int startno; int lastno; fib(int lastno,int startno) { this.startno=startno; this.lastno=lastno; } void disp() { System.out.println(startno); System.out.println(lastno); } }

Sub Class

class fibonacci extends fib { fibonacci(int l,int s) { super(s,l); super .disp(); } void disp() { int t1=startno,t2=t1+1; for (int i = 3; i <= lastno;i++) { int sum = t1 + t2; t1 = t2; t2 = sum; System.out.println(sum+" "); } } }

Main Method

import java.util.*; class main { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("Enter 2 numbers"); int a=sc.nextInt(); int b=sc.nextInt(); fibonacci obj=new fibonacci(a,b); obj.disp(); } }
        

Want to Clear Java Interview? Try our Java Interview Notes



Comments



Search
Recommended Deals End in










Recommended Quiz
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Java Program for Fibonacci Series using Inheritence

    Print Fibonacci Series using Java