fork(1) download
  1. #include <iostream>
  2. #include <math.h>
  3. #include <ctype.h>
  4.  
  5. using namespace std;
  6. int main()
  7. {
  8. int n=0,tot=0;
  9. float sum = 0;
  10. float average = 0;
  11. float product = 1;
  12.  
  13.  
  14. cout<<"Type an integer and press Enter:\n";
  15. cin>>n;
  16. /*
  17.   Your logic goes here
  18.   */
  19. for(int i=1;i<=n;i++){
  20. cout<<sum<<endl;
  21. sum= sum+(1.0/i);
  22. product=product*(1.0/i);
  23. tot++; // You don't need tot - you already have `n`
  24. }
  25. cout<<"Sum, product and average of reciprocals are:\n";
  26. cout<<sum<<endl;
  27. cout<<product<<endl;
  28. cout<<average<<sum/n<<endl;
  29. }
Success #stdin #stdout 0s 3464KB
stdin
4
stdout
Type an integer and press Enter:
0
1
1.5
1.83333
Sum, product and average of reciprocals are:
2.08333
0.0416667
00.520833