The Fibonacci sequence is defined by the following rules: The first two values in the sequence are 1 and 1. Every subsequent values is the sum of the two values preceding it. Write a java program that uses non recursive functions to print the nth value in the Fibonacci sequence.

Program Code:
import java.io.*;
import java.math.*;
class Fibonacci
{
int a=1,b=1,c;
int Fib(int no)
{
for(int i=1;i<=no-2;i++)
{
c=a+b;
System.out.print("\t"+c);
a=b;
b=c;
}
return c;
}
}
class Fibnonrec
{
public static void main(String[] args) throws Exception
{
int n,r;
System.out.println("Enter N value:");
DataInputStream dis=new DataInputStream(System.in);
n=Integer.parseInt(dis.readLine());
Fibonacci f=new Fibonacci();
System.out.print(f.a+"\t"+f.b);
r=f.Fib(n);
System.out.println("\nThe "+n+" the value in the Fibonacci Series : "+r);
}
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: