Write a program in java to find the sum of the given series: i) S = 1+1+2+3+5+....................to n terms ii) S = 2-4+6-8+...........................to n iii) S = a - a^3/5 + a^5 /9 – a^7 /13 .......................to n terms
Answers
Question 1:-
WAP in Java to find the sum of the given series.
1+2+3+4+5+.....N terms.
Code:-
import java.util.*;
class Series1
{
public static void main(String str[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms for the series: ");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n;i++)
s+=i;
System.out.println("Sum of the series: "+s);
}
}
Question 2:-
WAP in Java to find the sum of the series.
2-4+6-8+10-12+.......N
Code:-
import java.util.*;
class Series2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of terms for the series: ");
int n=sc.nextInt();
int s=0;
int a=1, b=2;
for(int i=1;i<=n;i++)
{
s=s+(b*a);
a*=-1;
b+=2;
}
System.out.println("Sum of the series is: "+s);
}
}
Question 3:-
WAP in Java to find the sum of the series.
Code:-
import java.util.*;
class Series3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a=sc.nextDouble();
System.out.print("Enter the value of n: ");
int n=sc.nextInt();
double s=0.0;
double x=1.0;
int b=1;
for(int i=1;i<=n;i++,b+=2,x+=4)
{
s=s+(Math.pow(-1,i+1)*Math.pow(a,b))/x;
}
System.out.println("Sum of the series is: "+s);
}
l