# Answer to Question #19371 in C++ for Bashayer

Question #19371

Write a program to solve the following equation

Y=1/2!-2/3!+3/4!-4/5!+….n/(n+1)!

Where n is entered by the user. For example if user enters 3 then your program should calculate Y as 1/2!-2/3!+3/4

Y=1/2!-2/3!+3/4!-4/5!+….n/(n+1)!

Where n is entered by the user. For example if user enters 3 then your program should calculate Y as 1/2!-2/3!+3/4

Expert's answer

#include <iostream>

#include <conio.h>

using namespace std;

double factorial(long n);

//main function

int main()

{

int n;

cout<<"Enter n= ";

cin>>n;

double sum=0;

for(int i=1;i<=n;i++){

if(i%2==0){

sum-=i/factorial(i+1);

}else{

sum+=i/factorial(i+1);

}

}

cout<<"Y= "<<sum;

getch();

return 0;

}

//find factorial

double factorial(long n){

if (n < 1)

return 0;

double product = 1;

for (int i = 1; i <= n; i++)

product *= i;

return product;

}

## Comments

## Leave a comment