title: How to fix this code of "1/0! + 1/1! + 1/2! +....+ 1/n!"
body: <p>So, I am trying to write a program to compute the value of the series 1/0! + 1/1! + 1/2! + .. + 1/n!. I think this is a pretty easy question to solve. But, the 1/0! i.e. the 1st term of the series is where it is creating all the problems for me. Please help me out. Please pardon for the silly mistakes if I have made since I started learning C++ 2 months ago and I am trying to solve various problems ever since.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int fact(int j)
  {
      int facto = 1;
      if (j == 0) {
          return facto;
      }
      else {
          for (int i = 2; i &lt;= j; i++) {
              facto = facto * i;
          }
          return facto;
      }
  }
  
  int main()
  {
  
      int n, p;
      float sum = 0, k;
      cout &lt;&lt; "Enter the value of n: " &lt;&lt; endl;
      cin &gt;&gt; n;
      for (int i = 0; i &lt; n; i++) {
          p = fact(i);
          cout &lt;&lt; p &lt;&lt; endl;
          k = 1 / p;
          cout &lt;&lt; k &lt;&lt; endl;
          sum = sum + k;
      }
      cout &lt;&lt; "Sum is: " &lt;&lt; sum &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>I have 2 questions :</p>
  
  <p>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.</p>
  
  <p>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 ??</p>