Computer Applications
Write the program in Java to find the sum of the following series:
S = 1 + 1 + 2 + 3 + 5 + ……. to n terms
Java
Java Iterative Stmts
87 Likes
Answer
import java.util.Scanner;
public class KboatSeries
{
public void computeSeriesSum() {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
int a = 1, b = 1;
int sum = a + b;
for (int i = 3; i <= n; i++) {
int term = a + b;
sum += term;
a = b;
b = term;
}
System.out.println("Sum=" + sum);
}
}
Variable Description Table
Program Explanation
Output
Answered By
33 Likes
Related Questions
Write the program in Java to display the first ten terms of the following series:
1, 11, 111, 1111,
Write the program in Java to display the first ten terms of the following series:
1, 12, 123, 1234,
Write the program in Java to find the sum of the following series:
S = 2 - 4 + 6 - 8 + ……. to n
Write the program in Java to find the sum of the following series:
S = 1 + (1+2) + (1+2+3) + ……. + (1+2+3+ ……. + n)