Wednesday, March 28, 2018

Fibonacci Code in C:

Outputs ("prints") the Fibonacci Sequence to any arbitrated number of places, granted that that number (the input) is an integer between 1 and 46. (Inputs 47 and over produce an Overflow for every other place starting with 47.) The Default (prior to input) is twenty places.

= = == === ===== ======== ============= ======== ===== === == = =

#include <stdio.h>
int main(void)
{
 /*     add more variable declarations */
 /* --> between here */
 int a, b, c, d;
 /* <-- and here */
 a = 0;
 b = 1;
 c = 0;
 int count;
 count = 20;
 /*     add code to print the first “count” Fibonacci numbers */
 /*     with the proper format */
 /* --> between here */
 while (count >= 0) {
  if (count % 2 == 0) {
   d = 1;
  }
  if (count % 2 != 0) {
   d = 0;
  }
  while (count > 0 && d == 0) {
   count = count - 1;
   if (count % 2 == 0) {
    if (count != 0) {
     printf("%d, ", a);
     c = a;
     b = a + b;
    }
    else {
     printf("%d.\n", a);
     scanf("%d", &count);
     a = 0;
     b = 1;
     c = 0;
     d = 1;
    }
   }
   else if (count % 2 != 0) {
    if (count != 0) {
     printf("%d, ", b);
     a = b + c;
    }
   }
  }
  while (count > 0 && d == 1) {
   count = count - 1;
   if (count % 2 != 0) {

    printf("%d, ", a);
    c = a;
    b = a + b;
   }
   else if (count % 2 == 0) {
    if (count != 0) {
     printf("%d, ", b);
     a = b + c;
    }
    else {
     printf("%d.\n", b);
     scanf("%d", &count);
     a = 0;
     b = 1;
     c = 0;
     d = 0;
    }
   }
  }
 }
 /* <-- and here */
 while (1) getchar();
 return 0;
}

Dm.A.A.

No comments:

Post a Comment