KnowledgeBoat Logo

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

Java

Java Iterative Stmts

ICSE

77 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);
    }
    
    
    
}

Output

BlueJ output of Write the program in Java to find the sum of the following series: S = 1 + 1 + 2 + 3 + 5 + ……. to n terms

Answered By

30 Likes