forked from sainirock61/CPP-Projects-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram7.cpp
More file actions
78 lines (66 loc) · 1.29 KB
/
Program7.cpp
File metadata and controls
78 lines (66 loc) · 1.29 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
#include<iostream>
using namespace std;
int main()
{
int n,even=0,odd=0;
float i,sum=0;
int no;
cout<<"Write a menu driven program to compute the sum of first n terms of the following series\n";
cout<<"\n";
cout<<" 1.1-2+3-4+5-6+7-8.....\n";
cout<<"\n";
cout<<" 2.1+1/2+1/3+1/4+1/5....\n";
cout<<"\n";
cout<<" Enter the number(1or2) so that the sum of which program compute first:"<<" ";
while(!(cin>>no)||(no!=1 && no!=2))
{
cin.clear();
cin.ignore(100,'\n');
cout<<"INVALID INPUT. ENTER AGAIN:";
}
cout<<"\n";
switch(no)
{
case 1:
cout<<" Enter the number of terms to which you want the sum="<<" ";
while(!(cin>>n)||n<=0)
{
cin.clear();
cin.ignore(100,'\n');
cout<<"INVALID INPUT. ENTER AGAIN:";
}
cout<<"\n";
i=1;
while(i<=n)
{
if((int)i%2==0)
{
even=even+i;
}
else
{
odd=odd+i;
}
i=i+1;
}
cout<<"Sum of "<<n<<" terms of series is="<<" "<<(odd-even);
break;
case 2:
cout<<" Enter the number of terms to which you want the sum="<<" ";
while(!(cin>>n)||n<=0)
{
cin.clear();
cin.ignore(100,'\n');
cout<<"INVALID INPUT! ENTER AGAIN";
}
cout<<"\n";
i=1;
while(i<=n)
{
sum=sum+(1.0)/i;
i=i+1;
}
cout<<"Sum of "<<n<<" terms of series is="<<" "<<sum;
break;
}
}