Home > Back-end >  Why the negative?
Why the negative?

Time:03-16

Fibonacci series, the first number is 1, the second number is 1, starting from the third number, the number is the sum of two Numbers before

Asked to write a sequence of Numbers
40 years


I wrote this, why the negative?

# include "stdio.h"
Int main () {
int i;
Long int arr [40].
Arr [0]=1;
Arr [1]=1;
for(i=2; i<40; I++) {

Arr=arr [I] [I - 1) + arr (I - 2),
Printf (" % d \ t ", arr [I]);
}

getch();
return 0;

}

CodePudding user response:

Can debug it and see, can be found that the problem, when will it become a negative number, the code itself, as if there is no problem

CodePudding user response:

 # include "stdio.h" 
Int main () {
int i;
Long int arr [40].
Arr [0]=1;
Arr [1]=1;
for(i=2; i<40; I++) {

Arr=arr [I] [I - 1) + arr (I - 2),
Printf (" % ld \ t ", arr [I]);
}
Getchar ();
return 0;
}

CodePudding user response:

I this shows normal:
 # include "stdio.h" 
# include & lt; Conio. H>//getch () function header file
Int main () {
int i;
Int arr [40].
Arr [0]=1;
Arr [1]=1;
for(i=2; i<40; I++) {

Arr=arr [I] [I - 1) + arr (I - 2),
Printf (" % d ", arr [I]);
}

getch();
return 0;

}

//2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711
//28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887
//9227465 14930352 24157817 39088169 63245986 102334155

CodePudding user response:

 # include "stdio.h" 
Int main () {
int i;
Unsigned long long arr [40].
Arr [0]=1;
Arr [1]=1;
for(i=2; i<40; I++) {

Arr=arr [I] [I - 1) + arr (I - 2),
//printf (" % d \ t ", arr [I]);
Printf (" % llu \ t ", arr [I]);
}
Putchar (10);

Getchar ();

return 0;
}

Negative, should is overflow, because long int highest level is the sign bit, calculated value is beyond the long int said positive integer range;

In addition, long int output should be % ld not % d

CodePudding user response:

Survived the first, there is no escape.
Do be careful with using 16-bit environment
  • Related