-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeries.cpp
More file actions
84 lines (76 loc) · 1.4 KB
/
Copy pathSeries.cpp
File metadata and controls
84 lines (76 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* WAP to compute the sum of the first n terms of the following series
S = 1+1/2+1/3+1/4+……
S = 1 + 1/2! + 1/3! + 1/4! +……
S =1-2+3-4+5………… */
#include <iostream>
using namespace std;
int calFact(int n) //Function to calculate Factorial
{
int fact=1;
for (int i=1;i<=n;i++)
{
fact*=i; //Calculating Factorial
}
return fact;
}
double series1(int n)
{
double sum=0,a=1;
for(int i=1;i<=n;i++)
{
sum+=a/i; //Calculate sum of series 1
}
return sum;
}
double series2(int n)
{
double sum=0,a=1;
for(int i=1;i<=n;i++)
{
sum+=a/(calFact(i)); //Calculate sum of series 1 by calculating factorial of i
}
return sum;
}
int series3(int n)
{
int sum=0;
for(int i=1;i<=n;i++)
{
if(i%2==0) //Checking for even.
sum+=(-i);
else
sum+=i;
}
return sum;
}
int main()
{
int n,ch;
cout<<"Enter a n limit of series"<<endl;
cin>>n; //Taking limit of series
Repeat: cout<<"Enter 1 for sum of series one.\n Enter 2 for sum of series two.\n Enter 3 for sum of series three."<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Sum of series1="<<series1(n);
break;
}
case 2:
{
cout<<"Sum of series2="<<series2(n);
break;
}
case 3:
{
cout<<"Sum of series3="<<series3(n);
break;
}
default:
{
cout<<"WRONG CHOICE"<<endl;
goto Repeat;
}
}
}