Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- int Fibonacci(int k){
- if (k == 0|| k == 1){
- return 1;
- }
- else{
- return Fibonacci(k - 1) + Fibonacci(k - 2);
- }
- }
- int main(){
- long N;
- FILE *in;
- in = fopen ("input.txt", "r");
- if (in == NULL){
- printf ("Invalid input");
- free(in);
- fclose(in);
- return 0;
- }
- fscanf(in, "%d", &N);
- if (N < 0){
- printf ("Invalid input");
- free(in);
- fclose(in);
- return 0;
- }
- fclose(in);
- FILE *out;
- out = fopen("output.txt", "w");
- if(out == NULL){
- printf ("Invalid input");
- free(out);
- fclose(out);
- return 0;
- }
- int *array;
- array = (int*)malloc(N * sizeof(int));
- if(array == NULL){
- printf ("Invalid input");
- free(array);
- return 0;
- }
- int i;
- for(i=0; i < N; i++){
- array[i] = Fibonacci(i);
- }
- for (i = N - 1 ; i >= 0; i--){
- fprintf(out, "%d", array[i]);
- if(i != 0){
- fprintf(out, "%s", " ");
- }
- }
- fclose(out);
- free(array);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement