Computer Applications
Write a program to find the sum of the given series:
S = (2/3) + (4/5) + (8/7) + --------- to n terms.
Java
Java Iterative Stmts
22 Likes
Answer
import java.util.Scanner;
public class KboatSeriesSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0.0;
for (int i = 1, j = 3; i <= n; i++, j+= 2)
sum += Math.pow(2, i) / j;
System.out.println("Sum = " + sum);
}
}
Variable Description Table
Program Explanation
Output
Answered By
12 Likes
Related Questions
Palindrome Number in Java: Write a program to accept a number from the user and check whether it is a Palindrome number or not. A number is a Palindrome which when reads in reverse order is same as in the right order.
Sample Input: 242
Sample Output: A Palindrome numberSample Input: 467
Sample Output: Not a Palindrome numberWrite a program to find the sum of the given series:
S = 1 + (1*2) + (1*2*3) + --------- to 10 terms.
The Greatest Common Divisor (GCD) of two integers is calculated by the continued division method. Divide the larger number by the smaller, the remainder then divides the previous divisor. The process repeats unless the remainder reaches to zero. The last divisor results in GCD.
Sample Input: 45, 20
Sample Output: GCD=5Write a program to generate a triangle or an inverted triangle based upon user's choice of triangle to be displayed.
Example 1:
Input: Type 1 for a triangle
Enter your choice: 1
Sample Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5Example 2:
Input: Type 2 for an inverted triangle
Enter your choice: 2
Sample Output:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1