kotlin 程序入口

In mathematics, the Fibonacci series is the series of the numbers where each number is the sum of the two preceding numbers, starting from 0 and 1.

在数学中, 斐波那契数列是数字的序列,其中每个数字是从0和1开始的两个先前数字的和。

Given two initial terms term1 and term2, we have to display the Fibonacci series till N terms.

给定两个初始项term1term2 ,我们必须显示斐波那契数列直到N个项。

Example:

例:

    Input:
    term1 = 0
    term2 = 1
    N = 10

    Output:
    Fibonacci series: 0 1 1 2 3 5 8 13 21 34

    Input:
    term1 = 0
    term2 = 1
    N = 5

    Output:
    Fibonacci series: 0 1 1 2 3

在Kotlin展示斐波那契数列的程序 (Program to display Fibonacci series in Kotlin)

/**
	* Display Fibonacci Series up to a Given number of terms
	* e.g.  0 1 1 2 3 5 8 13....n
*/

package com.includehelp.basic

import java.util.*

//Main Function entry Point of Program
fun main(args: Array<String>) {
	// Input Stream
	val scanner = Scanner(System.`in`)

	// input total number of terms
	println("Enter terms  : ")
	val n: Int = scanner.nextInt()

	var term1 = 0
	var term2 = 1
	var count = 1

	// Iterate Loop to print fibonacci Series upto given terms
	while (count <= n){
		print("$term1 ")
		val s = term1+term2
		term1 = term2;
		term2 = s
		count++
	}
}

Output

输出量

RUN 1:
Enter terms  : 
10
0 1 1 2 3 5 8 13 21 34 
---
Run 2:
Enter terms  : 
5
0 1 1 2 3


翻译自: https://www.includehelp.com/kotlin/display-fibonacci-series.aspx

kotlin 程序入口

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐