This blog is under construction

Friday 12 July 2013

C program to print non-Fibonacci numbers

Write a C program to print non-Fibonacci numbers.


  #include <stdio.h>

  /* returns 0 for non-fibonacci.  Otherwise, returns 1 */
  int isFibonacci(int data) {
        int num1 = 0, num2 = 1, temp, flag = 0;

        /* 0 and 1 are fibonacci numbers */
        if (data == num1 || data == num2) {
                return 1;
        }

        /* checking whether a given number is fobonacci no or not */
        while (num2 <= data) {
                temp = num2;
                num2 = num1 + num2;
                num1 = temp;
                if (num2 == data) {
                        return 1;
                }
        }

        return 0;
  }

  int main() {
        int i, n, flag;

        /* get the input from the user */
        printf("Enter the value for n:");
        scanf("%d", &n);

        /* print non-fibonacci numbers alone */
        for (i = 1; i <= n; i++) {
                flag = isFibonacci(i);
                if (!flag)
                        printf("%d ", i);
        }

        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:25
  4 6 7 9 10 11 12 14 15 16 17 18 19 20 22 23 24 25 



No comments:

Post a Comment