0

How to make the output show the first 100 elements of fibonacci series? What should I do?

#include <stdio.h> int main() { unsigned long long n, first = 0, second = 1, next, c; printf("Enter the number of terms\n"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-\n",n); for ( c = 1 ; c <= n ; c++ ) { next = first + second; first = second; second = next; printf("%llu\n",next); } return 0; } I made a code that was only able to make it show the correct elements from 1st to 93rd element. But from the 94th to 100th. The output is wrong.

13th Aug 2020, 3:38 PM
Louise Pabalan
2 Answers
+ 2
scanf("%d", &n); Here you read input for an `unsigned long long` variable using format specifier for `int`. I think variable <n> could've been just `int` opposed to `unsigned long long`, same goes with <c>. They are only there to count the loop. Maybe you can try by using `double'.
13th Aug 2020, 4:41 PM
Ipang
+ 1
That's because you are dealing with numbers that are big enough to not fit inside *int* data type. If you look closely your code is not even displaying 93 elements correctly as your value is again and again going out of range. I don't think C/C++ have any large enough primitive data type to store that huge numbers. For that you have to use arrays where each index of array is a digit of the big number
13th Aug 2020, 3:52 PM
Arsenic
Arsenic - avatar