Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java สำหรับตัวคูณที่ n ของตัวเลขใน Fibonacci Series


ในการหาตัวคูณที่ n ของตัวเลขในชุดฟีโบนักชี โค้ด Java มีดังต่อไปนี้ -

ตัวอย่าง

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));
   }
}

ผลลัพธ์

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

คลาสที่ชื่อว่า Demo มีฟังก์ชันชื่อ 'position' ซึ่งคำนวณตัวเลขฟีโบนักชี ในฟังก์ชันหลัก จำเป็นต้องกำหนดจำนวนที่มีตัวคูณในลำดับฟีโบนักชี ฟังก์ชันนี้เรียกด้วยพารามิเตอร์ที่เกี่ยวข้องและข้อมูลจะแสดงบนคอนโซล