write a java program to display the series 1/2+3/4+5/6+7/8
Answers
Answered by
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:
- START.
- Ask the user to enter the limit, say n.
- Assume a=1, b=2,s=0.
- Iterate a loop n times.
- Add the value of a/b to s.
- Increment the value of a and b by 2.
- Display the value of s after termination of loop.
- STOP.
See the attachment for output ☑.
Attachments:
Similar questions