This particular Code was made on Dcoder on Android... My question is,How am I still able to execute it if my input for n is less than 6..(Condition i>=6 is not fulfilled for the for loop right..)Also using this code I always get the answer as 1,2,3,5,8... and the number of terms printed is always more than the input of n..
Also I tried putting i<=0 but I get the same results...
#include <iostream>
using namespace std;
int a=0,b=1,x,i,n;
int main()
{
cout<<"This Program Gives You The List Of First 'n' Fibonacci Numbers:"<<endl
<<"Enter The Value Of 'n':"<<endl;
cin>>n;
if(n<1)
{
cout<<"Invalid Input"<<endl<<"Please Restart This Program And Enter A Natural Number."<<endl;
}
else
{
cout<<"The First "<<n<<" Fibonacci Numbers Are:"<<endl;
for(i;i>=6,i<=n;i++)
{
x=a+b;
a=b;
b=x;
cout<<x<<endl;
}
}
return 0;
}
But surprisingly the code below works..Why? And What is the fundamental difference between the two except that I intentionally print 0 and 1 in the second code...?Also I didn't find any difference when I used post increment and pre increment in my For Loop..Why?Also It would be really helpful to get some examples which behave differently with post and pre increment...
#include <iostream>
using namespace std;
int a=0,b=1,x,i,n;
int main()
{
cout<<"This Program Gives You The List Of First 'n' Fibonacci Numbers:"<<endl
<<"Enter The Value Of 'n':"<<endl;
cin>>n;
if(n<1)
{
cout<<"Invalid Input"<<endl<<"Please Restart This Program And Enter A Natural Number."<<endl;
}
else
{
cout<<"The First "<<n<<" Fibonacci Numbers Are:"<<endl;
cout<<"0"<<endl<<"1"<<endl;
for(i;i>=0,i<=n-2;i++)
{
x=a+b;
a=b;
b=x;
cout<<x<<endl;
}
}
return 0;
}