Saturday 28 March 2015


PROGRAM 15 : ENTER THE NUMBER OF TERMS OF PELL SERIES TO BE PRINTED

(Pell series is series of numbers which starts with 1 and then 2 and goes on.
 The next term is sum of double of precedent number and the number before the precedent number.)

Example :


'5' is the sum of double of '2' and '1' , i.e. , (1 + (2x2)). This way the series goes on.


PROGRAM :

import java.util.Scanner;
public class pellseries
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of terms in pell series:");
        int n = sc.nextInt();
        int a = 1 , b = 2 , t = 3 , c;
        System.out.print(a + " " + b); // FIRST TWO TERMS ARE PRINTED
        while(t<=n)
        {
            c = a + (2*b.);  //THIRD TERM IS SUM OF THE TWICE VALUE OF THE PRECEDENT VALUE OF THAT OF THE VALUE PRECEDING THE PREVIOUS VALUE ALSO
            a = b; // SWAPPING VALUES
            b = c;
            System.out.print(" " + c);
            t++; // INCREASING THE NUMBER OF TERMS BY ONE
            }
        }
    }

 

No comments:

Post a Comment