Fibonacci Solution | Java Language
The most famous Fibonacci problem solution in a java programming language is given below. IF YOU ARE A BEGINNER You will find it really helpful.
Theory: The idea of Fibonacci sires is that the third number is equal to the summation of the previous two numbers. Suppose 'a' = 0, and 'b' = 1, then the third number will be 'c' = 'a' + 'b'.
This means the sires will go like 0,1,1,2,3,5,8,13,21...
Input
--------------------------------------------
--------------------------------------------
package com.mycompany.basicjava; //THE NAME OF THE PACKAGE WILL NOT BE THE SAME
import java.util.Scanner; // COPY THIS IF IT IS NOT IMPORTED.
public class BasicJava { // PUBLIC CLASS MAYBE SOMETHING ELSE IN YOU IDE
/* ----------START COPYING CODE FROM HERE---------------*/
public static void main(String[] args) { /* CHECK IF YOU HAVE WROTE THIS TWICE, IF YES, REMOVE ONE OF THEM*/
int m, b = 1, a = 0, sum = 0,j =0;
Scanner input =new Scanner (System.in);
System.out.println("Enter a number place that you want to counte the fibonacci series : ");
m = input.nextInt();
j++;
System.out.println("The "+j+" number fibonacci is = "+a);
j++;
System.out.println("The "+j+" number fibonacci is = "+b);
for (int i = 1; i <m; i++) {
j++;
sum = a+b;
a = b;
b = sum;
System.out.println("The "+j+" number fibonacci is = "+sum);
}
/* ----------STOP COPYING HERE---------------*/
}
}
Comments
Post a Comment