Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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 : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot