c - Creating table for Fibonacci Sequence -
thank in advance. appreciate , feedback. new programming , working on assignment prints fibonacci sequence based on how many numbers user asks for. have of code complete, there 1 remaining piece having difficulty with. output in table format, off code , not getting of data in output. in grey code, output, , desired output.
#include <stdio.h> #include <stdlib.h> int main() { int i, n; int sequence = 1; int = 0, b = 1, c = 0; printf("how many fibonacci numbers print?: "); scanf("%d",&n); printf("\n n\t\t fibonacci numbers\n"); printf(" %d \t\t\t%d\n \t\t\t%d\n ", sequence, a, b); (i=0; <= (n - 3); i++) { c = + b; = b; b = c; sequence++; printf("\t\t\t%d\n ", c); } return 0; } here output: how many fibonacci numbers print?: 8 n fibonacci numbers 1 0 1 1 2 3 5 8 13 here desired output: how many fibonacci numbers print?: 8 n fibonacci numbers 1 0 2 1 3 1 4 2 5 3 6 5 7 8 8 13
i not getting of data
that's because not printing
sequence
inprintf()
offor
loop.printf("\t\t\t%d\n ", c);
and before 2nd number before
for
loopprintf(" %d \t\t\t%d\n \t\t\t%d\n ", sequence, a, b);
try making below changes code :
printf(" %d \t\t\t%d\n %d\t\t\t%d\n ", sequence, a, sequence+1, b); sequence++; //as you've printed 2 values in above printf (i=0; <= (n - 3); i++) { c = + b; = b; b = c; printf("%d\t\t\t%d\n ",++sequence, c); //or sequence++ before printf did , use sequence in printf }
sample input : 5
sample output :
how many fibonacci numbers print?: 5 n fibonacci numbers 1 0 2 1 3 1 4 2 5 3
edit : can using functions way... it's same thing :)
#include <stdio.h> void fib(int n) { int i,sequence=0,a=0,b=1,c=0; printf("\n n\t\t fibonacci numbers\n"); printf(" %d \t\t\t%d\n %d\t\t\t%d\n ", sequence, a, sequence+1, b); sequence++; (i=0; <= (n - 2); i++) { c = + b; = b; b = c; printf("%d\t\t\t%d\n ",++sequence, c); } } int main() { int n; printf("how many fibonacci numbers print?: "); scanf("%d",&n); fib(n); return 0; }
Comments
Post a Comment