Fibonacci sequence
//
// https://www.programiz.com/c-programming/online-compiler/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char* add_strings(char *a, char *b);
int main(int argc, char *argv[]) {
char x[1001]="0";
char y[1001]="1";
char *z;
int i;
int seq;
if (argc != 2) return 1;
if ((seq=atoi(argv[1])) == 0) return 1;
if (seq > 1000) return 1;
seq += 1;
printf("1:\t1\n");
for (i=2; i < seq; i++) {
z=add_strings(x,y);
strcpy(x,y);
strcpy(y,z);
printf("%d:\t%s\n", i,z);
}
return 0;
}
char* add_strings(char *a, char *b) {
char *r;
char tmp,carry;
int la=strlen(a);
int lb=strlen(b);
// swab a - b , so a is always bigger
if (la < lb) {
r = a;
a = b;
b = r;
la = strlen(a);
lb = strlen(b);
}
// result string is 1 bigger than a for carry bit
r = (char*)malloc((la+2) * (sizeof(char)));
r[la+1] ='\0';
carry=0;
int j = lb-1;
for (int i=la-1; i >=0; i--, j--) {
if (j >= 0) {
tmp = a[i] + b[j] - 96 + carry;
} else {
tmp = a[i] - 48 + carry;
}
r[i+1] = (tmp % 10) + 48;
if (tmp / 10 ) carry = 1; else carry = 0;
}
if (carry) r[0] = 49; else r = r + 1;
return r;
}