Question #16983

How static variable is different from the simple class variable?
WAP to add first seven terms of the following series:
1/1! + 2/2! + 3/3! +........
Explain the effect of static variable on the output of the program?

Expert's answer


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
static int sum = 0;
int factorial = 1;

for(int i = 1; i < 7; i++)
{
& for(int j = 1; j < i;j++) {
& factorial +=j;
& }
& sum += i/factorial;
& factorial = 0;
}
cout << sum << endl;
_getch();
}

LATEST TUTORIALS
APPROVED BY CLIENTS