Fibonacci Series using Inheritence in Java

(159 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(); } }

Solution Worked 4 UpvotesUpvote

        

Solution Didn't Worked 1 DownvotesDownvote



Comments



Search