Skip to main content

JAVA progs-2

The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence are 1, 1. Every subsequent value is the sum of the 2 values preceding it. Write a Java program that uses both recursive and non-recursive functions to print the nth value of the Fibonacci sequence.

/*Non Recursive Solution*/
import java.util.Scanner;
class Fib {
public static void main(String args[ ])
{
Scanner input=new Scanner(System.in);
int i,a=1,b=1,c=0,t;
System.out.println("Enter value of t:");
t=input.nextInt();
System.out.print(a);
System.out.print(" "+b);
for(i=0;i<t-2;i++) {
c=a+b;
a=b;
b=c; System.out.print(" "+c);
}
 System.out.println();
System.out.print(t+"th value of the series is: "+c);
}
}
Expected Input and output: 
Enter the value of t: 5 1 1 2 3 5
Actual Input & Output : 


JAVA progs-2

For more info regarding technology and pro stuff...visit http://peoplezmind.com/

Comments

  1. view-source:http://127.0.0.1:8889/e2b39ccf-5e87-47e8-99cf-f7a7b36d810d
    http://127.0.0.1:8889/2e1abcd9-d8b9-4c57-b172-f5afb36e065a

    ReplyDelete

Post a Comment

Popular posts from this blog

CPU Schedueling programs

        Fcfs program         #include<stdio.h> #include<conio.h> main() { int bt[20], wt[20], tat[20], i, n; float wtavg, tatavg; printf("\nEnter the number of processes -- "); scanf("%d", &n); for(i=0;i<n;i++) { printf("\nEnter Burst Time for Process %d -- ", i); scanf("%d", &bt[i]); } wt[0] = wtavg = 0; tat[0] = tatavg = bt[0]; for(i=1;i<n;i++) { wt[i] = wt[i-1] +bt[i-1]; tat[i] = tat[i-1] +bt[i]; wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i]; } printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n"); for(i=0;i<n;i++) printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n); getch(); } Output SJF program #include<stdio.h> #include<conio.h> main() { int p[20], bt[20], wt[20],...