To find the nth multiple of a number in Fibonacci series, the Java code is as follows −
Example
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
A class named Demo contains the function named ‘position’ that calculates the Fibonacci numbers. In the main function, the number whose multiple in the Fibonacci sequence needs to be is defined. The function is called with relevant parameters and the data is displayed on the console.