Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int fact(int j)
  5. {
  6. int facto = 1;
  7. if(j==0)
  8. {
  9. return facto;
  10. }
  11. else
  12. {
  13. for(int i=2;i<=j;i++)
  14. {
  15. facto = facto * i;
  16. }
  17. return facto ;
  18. }
  19. }
  20.  
  21.  
  22. int main()
  23. {
  24.  
  25. int n, p ;
  26. float sum = 0, k ;
  27. cout << "Enter the value of n: " << endl;
  28. cin >> n;
  29. for(int i=0; i<n; i++)
  30. {
  31. p = fact(i);
  32. cout << p << endl;
  33. k = 1/p ;
  34. cout << k << endl;
  35. sum = sum + k;
  36. }
  37. cout << "Sum is: " << sum << endl;
  38. return 0;
  39. }
  40.  
  41. 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.
  42.  
  43. 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