kotlin 计算平方_Kotlin程序计算自然数之和
生活随笔
收集整理的這篇文章主要介紹了
kotlin 计算平方_Kotlin程序计算自然数之和
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
kotlin 計算平方
Given a number number, and we have to calculate the sum of all natural numbers from 1 to number.
鑒于一些數字 ,我們必須從1計算所有自然數的總和數量 。
Example:
例:
Input:number = 15Output:120用于計算Kotlin中自然數之和的程序 (Program to calculate the sum of natural numbers in Kotlin)
package com.includehelp.basicimport java.util.*//Main Function entry Point of Program fun main(args: Array<String>) {//Input Streamval scanner = Scanner(System.`in`)//input integer numberprint("Enter Number : ")val number: Int = scanner.nextInt()var sum=0//Iterate through loop to calculate sumfor(i in 1..number){sum+=i}//Print Sumprintln("Sum of All Natural Numbers up to $number are : $sum") }Output
輸出量
Run 1: Enter Number : 15 Sum of All Natural Numbers up to 15 are : 120 --- Run 2: Enter Number : 99 Sum of All Natural Numbers up to 99 are : 4950 --- Run 3: Enter Number : 456 Sum of All Natural Numbers up to 456 are : 104196翻譯自: https://www.includehelp.com/kotlin/calculate-the-sum-of-natural-numbers.aspx
kotlin 計算平方
總結
以上是生活随笔為你收集整理的kotlin 计算平方_Kotlin程序计算自然数之和的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一亩地赔偿多少钱啊?
- 下一篇: 联想ThinkPad T510(i5