Computer Science, asked by Anonymous, 4 months ago

Write a program in Java to find the sum of the given series
 \frac{ {x}^{1} }{ {y}^{2} }  +  \frac{ {x}^{3} }{ {y}^{4} }  +  \frac{ {x}^{5} }{ {y}^{6} } ...n \: terms

Answers

Answered by anindyaadhikari13
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?
anindyaadhikari13: I use pyroid 3 for showing the output.
anindyaadhikari13: Sorry, ignore that, I use Java N-IDE
Anonymous: ok
anindyaadhikari13: and for beautifying, go to this site carbon . now . sh
anindyaadhikari13: i have written spaces between the link otherwise this comment will contain link.
Anonymous: ha I can understand
Similar questions