0

What's wrong with the code.. if I enter n value as 1 or 2, it prints some garbage value.. why it is so ?

#include<stdio.h> #include<math.h> int main() { int n, i,third; int f=0, s=1; printf("enter the position to print the fib number: "); scanf("%d",&n); if(n==1) { printf("%d",f); } else if(n==2) { printf("%d",s); } else { for(int i=3; i<=n; i++) { third=f+s; f=s; s=third; } } printf("%d",third); return 0; }

25th Mar 2021, 3:40 AM
Ashu Williams
Ashu Williams - avatar
1 Resposta
+ 4
The garbage value is the value of variable *third* that you are printing at the end. A simple fix is to move it inside the else statment https://code.sololearn.com/cvUQqkXtmN1u/?ref=app
25th Mar 2021, 4:05 AM
Arsenic
Arsenic - avatar