Nth Multiple of a Number in Fibonacci Series




In this article, we will learn how to find the nth multiple of a given number in the Fibonacci series using Java. The Fibonacci series is a sequence where each number is the sum of the two preceding ones. We will use loops, conditional statements, and the modulus operator to find the position of the nth multiple of a specific number in the series. 

Problem Statement

Given a number and its multiple positions, find the position of the nth multiple of a number in the Fibonacci series.

Input

A number whose multiple is to be found: 9
The nth occurrence of the multiple to be found: 10

Output
Position of 10th multiple of 9 in the Fibonacci series = 120

Steps to find the nth multiple of a number in the Fibonacci series

The following are the steps to find the nth multiple of a number in the Fibonacci series

  • Initialize two variables representing the first two numbers of the Fibonacci series: 0 and 1.
  • Use a while loop to generate Fibonacci numbers by summing the previous two numbers.
  • Check if the current Fibonacci number is a multiple of the given number.
  • If the Fibonacci number is a multiple, increment a counter.
  • When the counter reaches the nth multiple, return the current position.
  • Print the final position of the nth multiple in the Fibonacci series

Java program to find the nth multiple of a number in the Fibonacci series

The following is an example of finding the nth multiple of a number in Fibonacci series:

public class Demo{
   public static int position(int pos, int num){
      long val_1 = 0, val_2 = 1, val_3 ;
      int i = 2;
      while(i != 0){
         val_3 = val_1 + val_2;
         val_1 = val_2;
         val_2 = val_3;
         if(val_2 % pos == 0){
            return num * i;
         }
         i++;
      }
      return 0;
   }
   public static void main(String[] args){
      int n = 10;
      int k = 9;
      System.out.print("Position of 10th multiple of 9 in the Fibonacci number list is ");
      System.out.println(position(k, n));
   }
}

Output

Position of 10th multiple of 9 in the Fibonacci number list is 120

Code Explanation

The program defines a method position that uses a while loop to generate Fibonacci numbers by updating two variables. The if statement checks if the current Fibonacci number is divisible by the given number. When it finds the nth multiple, the method returns the position. The main method calls the position method, passing in the values of n and k, and uses System.out.println to print the position of the Fibonacci sequence.

Updated on: 2024-11-05T22:02:55+05:30

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements