Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

10,484 questions

14,262 answers

573 users

How to print all int array elements that are fibonacci numbers in C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>

int arr_max(int arr[], int len) {
	int mx = arr[0];
	
	for (int i = 1; i < len; i++) {
		if (arr[i] > mx)
			mx = arr[i];
	}
	return mx;
}

bool exist(int arr[], int len, int n) {
	for (int i = 0; i < len; i++) {
		if (arr[i] == n)
			return true;
	}
	return false;
}
 
void fibonacci_numbers(int arr[], int len) { 
    int mx = arr_max(arr, len);
    
    int a = 0, b = 1;
	if (exist(arr, len , a))
		printf("%4d", a);

    do {
        int c = a + b; 
		if (exist(arr, len , c)) {
			printf("%4d", c);
		}
        a = b; 
        b = c; 
    } while (b <= mx);
	printf("\n");
} 
    
int main() 
{ 
    // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
    int arr[] = {9, 1, 31, 12, 13, 3, 89, 100, 233, 4, 144, 99}; 
    int len = sizeof(arr) / sizeof(arr[0]); 
      
    fibonacci_numbers(arr, len); 
      
    return 0; 
} 
  
  
  
/*
run:
  
   1   3  13  89 144 233
  
*/

 




answered May 23 by avibootz
...