Computer Applications
Write the program to find the sum of the following series:
S = a - a3 + a5 - a7 + ……. to n
Java
Java Iterative Stmts
19 Likes
Answer
import java.util.Scanner;
public class KboatSeries
{
public void computeSeriesSum() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
int sum = 0;
for (int i = 1, j = 1; i <= n; i = i + 2, j++) {
if (j % 2 == 0)
sum -= Math.pow(a, i);
else
sum += Math.pow(a, i);
}
System.out.println("Sum=" + sum);
}
}
Variable Description Table
Program Explanation
Output
Answered By
7 Likes
Related Questions
Write a program to enter two numbers and check whether they are co-prime or not.
[Two numbers are said to be co-prime, if their HCF is 1 (one).]
Sample Input: 14, 15
Sample Output: They are co-prime.Write the program to find the sum of the following series:
S = (1/a) + (2/a2) + (3/a3) + ……. to n
Write a program to input a number. Display the product of the successors of even digits of the number entered by user.
Input: 2745
Output: 15
[Hint: The even digits are: 2 and 4
The product of successor of even digits is: 3*5= 15]Write the program to find the sum of the following series:
S = (a/2) + (a/5) + (a/8) + (a/11) + ……. + (a/20)