fibonacci series using C++ for and if else loop
#include<iostream>
using namespace std;
int main()
{
int i,j,t1=0,t2=1, nexTerm=0;
cout<<"enter the number of terms ";
cin>>j;
for(i=1;i<=j;++i)
{
if(i==1)
{
cout<<t1<<",";
continue;
}
if(i == 2) {
cout<<t2<<",";
continue;
}
nexTerm = t1+t2;
t1=t2;
t2 = nexTerm;
cout<<nexTerm<<",";
}
return 0;
}
Comments
Post a Comment