c - Fibonacci series sum -
i have following code:
#include<stdio.h> void main() { int = -1, b = 1, c = 0, i, n, sum = 0 ; printf("enter limit : ") ; scanf("%d", &n) ; printf("\nthe fibonacci series :") ; for(i = 1 ; <= n ; i++) { c = + b ; printf("%d-", c) ; sum = sum + c ; = b ; b = c ; } printf("\nthe sum of fibonacci series : %d", sum) ; printf("\n"); }
now need find term of series makes sum bigger 7 digits, how can that? p.s new c
, sorry english.
if want first sum of 7 digits, set threshold 1 million, known 1e6 = 10**6 = 1000000
. notice there 7 digits in 1000000
.
if want sum have more 7 digits, set threshold ten million. number has 8 digits.
the following code trick. note instead of for
loop, have while
loop keeps iterating while sum less threshold.
# include <stdio.h> int main() { int previous, current = 0, next = 1, sum = current, threshold; printf("enter threshold: ") ; scanf("%d", &threshold) ; printf("fibonacci series: %d", current) ; while (sum < threshold) { previous = current; current = next; next = previous + current; printf(" + %d", current) ; sum += current; } printf(" = %d\n", sum); return 0; }
Comments
Post a Comment