- Home
- Java Series Programs
Write the program in Java to find the sum of the following series S 1
Java Series Programs
Write the program in Java to find the sum of the following series:
S = 1 + 1 + 2 + 3 + 5 + ……. to n terms
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);
}
}