Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int fact(int j)
- {
- int facto = 1;
- if(j==0)
- {
- return facto;
- }
- else
- {
- for(int i=2;i<=j;i++)
- {
- facto = facto * i;
- }
- return facto ;
- }
- }
- int main()
- {
- int n, p ;
- float sum = 0, k ;
- cout << "Enter the value of n: " << endl;
- cin >> n;
- for(int i=0; i<n; i++)
- {
- p = fact(i);
- cout << p << endl;
- k = 1/p ;
- cout << k << endl;
- sum = sum + k;
- }
- cout << "Sum is: " << sum << endl;
- return 0;
- }
- 1) When I am giving 0 as input, I am getting 0(i.e. the value of sum) as output(but, I am expecting to get 1) and, when I am giving 1 as input, it is giving 1 as output(expecting 2 as output). Please help me in pointing out the loophole.
- 2) When I am giving 0 as input, it is not printing the values of 'p' and 'k' but any input greater than 0 is showing the values of 'p' and 'k' for each time it completes the loop. Why ??
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement