Write a C++ program to illustrate the static variable functionality using the sum of a Fibonacci series as an example.
Program Code:
#include<iostream.h>
class item
{
public:
static int sum;
void fibo(int f,int s)
{
if(s>100)
cout<<"Sum="<<sum<<endl;
else
{
sum+=s;
fibo(s,f+s);
}
}
};
int item::sum;
int main()
{
item a;
a.sum=1;
a.fibo(1,1);
return 0;
}
Post A Comment:
0 comments: