Posted by : Unknown Sunday, 28 June 2015

import java.util.Scanner;
class fibonacci
{
  public static void main(String[] args)
    {
        System.out.print("Enter the number to display fibonicci series:  ");
        Scanner in=new Scanner(System.in);
        int num=in.nextInt();
        int a=0,b=1,c=0,i=0;
        while(i!=num)
        {
            System.out.println(c);
            a=b;
            b=c;
            c=a+b;
            i++;         
        }
    }
}

Output:

{ 5 comments... read them below or Comment }

  1. GIVE ME CODE FOR CLIENT AND SERVER FOR fibonacci-series-using- JAVA
    URGENT PLZZ

    ReplyDelete
    Replies
    1. RMI Fibonacci Program


      FIBONACCI SERVER :

      import java.net.*;
      import java.rmi.*;

      public class FibonacciServer
      {
      public static void main(String[] args)
      {
      try
      {
      FibonacciImpl f=new FibonacciImpl();
      Naming.rebind(“fibonacci”,f);
      System.out.println(“Fibonacci server ready.”);
      }
      catch(RemoteException re)
      {
      System.out.println(“Exception in FibonacciImpl.main: ”+re);
      }
      catch(MalformedURLException e)
      {
      System.out.println(“Malformed URL Exception ”+e);
      }
      }
      }


      FIBONACCI INTERFACE :

      import java.rmi.*;
      import java.math.BigInteger;

      public interface Fibonacci extends Remote
      {
      public BigInteger getFibonacci(int n) throws RemoteException;
      public BigInteger getFibonacci(BigInteger n) throws RemoteException;
      }


      FIBONACCI CLIENT :

      import java.rmi.*;
      import java.net;
      import java.math.BigInteger;

      public class FibonacciClient
      {
      public static void main(String args[])
      {
      if(args.length = = 0 || !args[0].startsWith(“rmi:”))
      {
      System.err.println(“Usage:java Fibonacci client rmi://host.domain:port/fibonacci number”);
      return();
      }
      try
      {
      Object o = Naming.lookup(args[0]);
      Fibonacci calculator = (Fibonacci)o;
      for(int i=1;i<args.length;i++)
      {
      try
      {
      BigInteger index = new BigInteger (args[i]);
      BigInteger f = calculator.getFibonacci(index);
      System.out.println(“The ”+args[i]+” Fibonacci number is “+f);
      }
      catch(NumberFormatException e)
      {
      System.err.println(args[i]+”is not an integer.”);
      }
      }
      }
      catch(MalformedURLException e)
      {
      System.err.println(args[0]+” is not a valid RMI URL.”);
      }
      catch(RemoteException e)
      {
      System.out.println(“Remote object threw exception ”+e);
      }
      catch(NotBoundException e)
      {
      System.err.println(“Could not find the requested remote object on server.”);
      }
      }
      }


      FIBONACCI IMPLEMENATATION :

      import java.rmi.*;
      import java.rmi.server.UnicastRemoteObject
      import java.math.BigInteger;

      public class FibonacciImpl implements Fibonacci
      {
      public FibonacciImpl() throws RemoteException
      {
      UnicastRemoteObject.exportObject(this);
      }

      public BigInteger getFibonacci(int n) throws RemoteException
      {
      return this.getFibonacci(new BigInteger(Long.toString(n)));
      }

      public BigInteger getFibonacci (BigInteger n) throws RemoteException
      {
      System.out.println(“Calculating the ”+n+”the Fibonacci number.”);
      BigInteger zero=new BigInteger(“0”);
      BigInteger one=new BigInteger(“1”);
      if(n.equals(zero))
      return zero;
      if(n.equals(one))
      return one;
      BigInteger i=one;
      BigInteger a=zero;
      BigInteger b=one;
      while(i.compareTo(n)= = -1)
      {
      BigInteger temp = b;
      b=b.add(a);
      a=temp;
      i=i.add(one);
      }
      return b;
      }
      }

      Delete
    2. Here is the code:

      FibClient.java

      package fib;
      import java.rmi.Naming;
      import java.io.*;

      public class FibClient {

      public static void main(String args[]) {
      try {
      int numFibNum;
      String registryURL = "rmi://localhost:1099/fib";
      FibInterface h1 = (FibInterface)Naming.lookup(registryURL);
      numFibNum = 5;
      Fib c = new Fib();
      //numFibNum = Integer.parseInt(args[0]);
      for (int i=0; i<numFibNum; i++) {
      h1.getNextFibNum(c);
      System.out.print(h1.getNextFibNum(new Fib())+" ");
      }
      System.out.println();

      for (int i=0; i<numFibNum; i++) {
      System.out.print(h1.getNextFibNum(h1.getFib(0,1))+" ");
      }
      System.out.println();

      Fib f = new Fib();
      for (int i=0; i<numFibNum; i++) {
      System.out.print(h1.getNextFibNum(f)+" ");
      }
      System.out.println();

      FibImpl h2 = new FibImpl();
      for (int i=0; i<numFibNum; i++) {
      System.out.print(h2.getNextFibNum(f)+" ");
      }
      System.out.println();

      f = new Fib();
      for (int i=0; i<numFibNum; i++) {
      f = h1.getNextFib(f);
      System.out.print(f.getF1()+" ");

      }

      System.out.println();
      } catch(Exception ex) {
      ex.printStackTrace();
      }
      }

      }




      FibImpl.java

      package fib;
      import java.rmi.RemoteException;
      import java.rmi.server.UnicastRemoteObject;
      @SuppressWarnings("serial")
      public class FibImpl extends UnicastRemoteObject implements FibInterface {
      public FibImpl() throws RemoteException {
      super();
      }
      public Fib getFib(int f0, int f1) throws RemoteException {
      return new Fib(f0, f1);
      }
      public int getNextFibNum(Fib f) throws RemoteException {
      sleep(500);
      int nextFib = f.getF0() + f.getF1();
      f.setF0(f.getF1());
      f.setF1(nextFib);
      return nextFib;
      }
      public Fib getNextFib(Fib f) throws RemoteException {
      sleep(500);
      int nextFib = f.getF0() + f.getF1();
      f.setF0(f.getF1());
      f.setF1(nextFib);
      return f;
      }
      private void sleep(int time) {
      try {
      Thread.sleep(time);
      }
      catch(InterruptedException ex) {
      ex.printStackTrace();
      }
      }
      }





      Fib.java

      package fib;
      import java.io.Serializable;
      @SuppressWarnings("serial")
      public class Fib implements Serializable {
      private int f0;
      private int f1;
      public Fib() {
      this(0,1);
      }
      public Fib(int f0, int f1) {
      this.f0 = f0;
      this.f1 = f1;
      }
      public int getF0() {
      return f0;
      }
      public void setF0(int f0) {
      this.f0 = f0;
      }
      public int getF1() {
      return f1;
      }
      public void setF1(int f1) {
      this.f1 = f1;
      }
      }



      The result will be displayed like below.

      1 1 1 1 1

      1 1 1 1 1

      1 1 1 1 1

      1 2 3 5 8

      1 2 3 5 8

      Delete
    3. Here is the code for linux system:

      Launching the RMI registry on linux by using the command rmiregistry & and it starts without problems.



      FibonacciServer.java:

      package myrmifibonacciserver;

      import java.net.MalformedURLException;
      import java.rmi.Naming;
      import java.rmi.RemoteException;

      public class FibonacciServer {
      public static void main(String args[]){
      try{
      Fibonacci fib = new Fibonacci();
      Naming.rebind("fibonacci", fib);
      System.out.println("Fibonacci Server ready.");
      }catch(RemoteException rex){
      System.err.println("Exception in Fibonacci.main " + rex);
      } catch (MalformedURLException ex) {
      System.err.println("MalformedURLException " + ex);
      }
      }
      }



      Fibonacci:

      package myrmifibonacciserver;

      import java.math.BigInteger;
      import java.rmi.RemoteException;
      import java.rmi.server.UnicastRemoteObject;

      public class Fibonacci extends UnicastRemoteObject implements IFibonacci{

      private static final long serialVersionUID = -4300545841720809981L;

      public Fibonacci() throws RemoteException{
      super();
      }

      @Override
      public BigInteger getFibonacci(int n) throws RemoteException {
      return getFibonacci(new BigInteger(Long.toString(n)));
      }

      @Override
      public BigInteger getFibonacci(BigInteger n) throws RemoteException {
      System.out.println("Calculating teh " + n + "th Fibonacci number");
      BigInteger zero = BigInteger.ZERO;
      BigInteger one = BigInteger.ONE;

      if(n.equals(zero) || n.equals(one))
      return one;

      BigInteger current = one;
      BigInteger low = one;
      BigInteger high = one;
      BigInteger temp;

      while(current.compareTo(n) == -1){
      temp = high;
      high = high.add(low);
      low = temp;
      current = current.add(one);
      }
      return high;
      }

      }



      IFibonacci:

      package myrmifibonacciserver;

      import java.math.BigInteger;
      import java.rmi.Remote;
      import java.rmi.RemoteException;

      public interface IFibonacci extends Remote{
      public BigInteger getFibonacci(int n) throws RemoteException;
      public BigInteger getFibonacci(BigInteger n) throws RemoteException;
      }

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Welcome to My Blog

Translate

Popular Post

Total Pageviews

Blog Archive

- Copyright © Learning Java Program - Powered by Blogger -