Pin It

Write a Java program to print Padovan Sequence.

Program to implement Padovan sequence in Java.

The Padovan Sequence starts with three 1's and after that moves on with a function as : f(x) = f(x-2)+ f(x-3).

package com.hubberspot.java.sequences;

import java.util.Scanner;

public class Padovan {

    public static void main(String[] args) {
        
        int firstNumber = 1;
        int secondNumber = 1;
        int thirdNumber = 1;
                
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the number of terms for Padovan sequence : ");
        
        int noOfTerms = scanner.nextInt();
        int nextNumber;
        
        System.out.print( firstNumber + " " + secondNumber + " " + thirdNumber);
        
        for(int i = 1; i <= noOfTerms - 3; i++) { 
            
            nextNumber = secondNumber + firstNumber;
            System.out.print(" " + nextNumber);
            firstNumber = secondNumber;
            secondNumber = thirdNumber;
            thirdNumber = nextNumber;
            
        }        


    }

}


Output of the program : 


 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot