Computer Science, asked by Ritambhar, 4 months ago

write a java program to display the series 1/2+3/4+5/6+7/8​

Answers

Answered by anindyaadhikari13
6

Required Answer:-

Question:

  • WAP in Java to display the sum of the series - 1/2 + 3/4 + 5/6 +...

Solution:

Here comes the program.

import java.util.*;

public class Sum {

 public static void main(String[] args) {

     int n, i;

     double s=0.0, a=1.0, b=2.0;

     Scanner sc=new Scanner(System.in);

     System.out.print("Enter limit: ");

     n=sc.nextInt();

     for(i=1;i<=n;i++,a+=2,b+=2)

        s+=a/b;

     System.out.println("Sum: "+s);

     sc.close();

 }

}

Algorithm:

  1. START.
  2. Ask the user to enter the limit, say n.
  3. Assume a=1, b=2,s=0.
  4. Iterate a loop n times.
  5. Add the value of a/b to s.
  6. Increment the value of a and b by 2.
  7. Display the value of s after termination of loop.
  8. STOP.

See the attachment for output ☑.

Attachments:
Similar questions