[Not a challenge but urgent]
Programming language: Java
WAP to store 25 numbers in an array and display only those numbers which are PRESENT in Fibbonacci Series.
Thanks. :)
Spams will be deleted.
Answers
Solution:
The given code is written in Java.
import java.util.*;
public class Array {
public static void main(String s[]) {
Scanner sc=new Scanner(System.in);
int ELEMENTS=25,i;
int y[]=new int[ELEMENTS];
System.out.println("Enter 25 elements in the array...");
for(i=0;i<ELEMENTS;i++){
System.out.print("a["+i+"] >> ");
y[i]=sc.nextInt();
}
System.out.println("\nGiven Array: "+Arrays.toString(y));
System.out.println("Fibonacci Numbers are as follows - ");
for(int x:y){
int a=1,b=0,c=0;
boolean isFibonacci=false;
for(;c<=x;c=a+b,a=b,b=c){
if(c==x)
isFibonacci=true;
}
if(isFibonacci)
System.out.print(x+" ");
}
}
}
Logic:
- Loop through the numbers in the array.
- Initialise a=1,b=0 and c=0.
- Now, assume that the number is not a fibonacci number, isFibonacci=false.
- Now, create another loop that checks whether the number is fibonacci number or not. If true, make isFibonacci=true.
- After termination of inner loop, check if isFibonnacci==true. If true, print the number.
See the attachment for output.