Skip to main content

Java program to print fibonacci series in reverse order


import java.util.*;
public class FabonicRev {
    Scanner scan;
    int a1, b1, c1, a2, b2, c2, limit, i;
    public FabonicRev() {
        scan = new Scanner(System.in);
        a1 = c1 = 1;
        b1 = a2 = b2 = c2 = limit = i = 0;
    }
    public void input() {
        try {
            System.out.printf("Enter limit: ");
            limit = scan.nextInt();
            scan.close();
        } catch (InputMismatchException | NumberFormatException e) {
            System.err.println("Error Occurred!");
            System.exit(0);
        }
    }
    public void compute() {
        for (; i < limit; i++) {
            a1 = b1;
            b1 = c1;
            c1 = a1 + b1;
            a2 = c1;
            c2 = b1;
        }
        System.out.println("Series in Reverse Order:");
        System.out.println(a2 + "\n" + c2);
        for (i = limit - 1; i >= 0; i--) {
            b2 = a2;
            a2 = c2;
            c2 = b2 - a2;
            System.out.println(c2);
        }
    }
    public static void main(String[] args) {
        FabonicRev fr = new FabonicRev();
        fr.input();
        fr.compute();
    }
}

I've excluded first two numbers from the limit.
Output:-
Enter limit: 10
Series in Reverse Order:
89
55
34
21
13
8
5
3
2
1
1
0

Comments

Shivam said…
Hey Ashutosh Thanks for sharing the logic.

but try the corrected logic. (Commented lines are improved.)

package fibonacci;

import java.util.Scanner;

public class ReverseFibonacci2
{
static Scanner in = new Scanner(System.in);
static int a1 = 1;
static int a2 = 0;
static int b1 = 0;
static int b2 = 0;
static int c1 = 1;
static int c2 = 0;
static int limit;
static int i;

public static void main(String[] args)
{
input();
compute();
}

static void input()
{
System.out.print("Enter limit: ");
limit = in.nextInt();
in.close();
}

static void compute()
{
for (; i < limit; i++)
{
a1 = b1;
b1 = c1;
c1 = a1 + b1;
a2 = c1;
c2 = b1;
}
System.out.println("Reverse fibonacci series is:-");
// System.out.print(a2 + "\t" + c2);
// for (i = limit - 1; i > 0; i--)
for (i = limit - 1; i >= 0; i--)
{
b2 = a2;
a2 = c2;
c2 = b2 - a2;
System.out.print(c2 + "\t");
}
}
}

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...