Write a program in Java to find the sum of the given series
Answers
Answered by
6
Required Answer:-
Question:
Write a Java program to find out the sum of the series.
S = x¹/y² + x³/y⁴ +...N terms.
Solution:
Here comes the program.
import java.util.*;
public class Sum {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n, a=1, b=2;
double x, y, s=0;
System.out.print("Enter limit - ");
n=sc.nextInt();
System.out.print("Enter x - ");
x=sc.nextDouble();
System.out.print("Enter y - ");
y=sc.nextDouble();
for(int i=1;i<=n;i++,a+=2,b+=2)
s+=Math.pow(x,a)/Math.pow(y, b);
System.out.println("Sum: "+s);
sc.close();
}
}
Explanation:
- We will ask the user to enter the limit and values of x and y. Each term is in the form of x^a/y^b where a={1,3,5,...} and b={2,4,6,8,...}. So, we have created two variables a and b. Initially, a = 1 and b = 2. We will create a loop that iterates n times. Inside the loop, we will compute x^a/y^b and add them to the sum variable. After each iteration, values of a and b are incremented by 2.
Sample I/O:
Enter Limit - 5
Enter x - 2
Enter y - 1
Sum: 682.0
Attachments:
Anonymous:
Which app do you use to do these all?
Similar questions
Computer Science,
2 months ago
World Languages,
2 months ago
Math,
2 months ago
Physics,
4 months ago
English,
4 months ago
Social Sciences,
10 months ago
Math,
10 months ago