kotlin 程序入口_Kotlin程序将展示斐波那契数列

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 程序入口

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值