-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfSeries.java
More file actions
78 lines (73 loc) · 1.51 KB
/
Copy pathSumOfSeries.java
File metadata and controls
78 lines (73 loc) · 1.51 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
import java.util.Scanner;
public class SumOfSeries {
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;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
SumOfSeries ob=new SumOfSeries();
System.out.println("Enter a limit n of the series");
int n=sc.nextInt();
System.out.println("Enter the choice \n1 for Sum of series 1 \n2 for Sum of series 2 \n3 for Sum of series 3");
int ch=sc.nextInt();
switch(ch)
{
case 1:
{
System.out.println("Sum of series1="+ob.series1(n));
break;
}
case 2:
{
System.out.println("Sum of series2="+ob.series2(n));
break;
}
case 3:
{
System.out.println("Sum of series3="+ob.series3(n));
break;
}
default:
{
System.out.println("Wrong Choice entered,the program will exit now");
}
}
}
}