Kotlin Program to Find Sum of N Natural Numbers

In this post, we will go through Kotlin program to find sum of n natural numbers.

What is natural number?

Positive numbers, e.g. 1,2,3,4,5,6…etc., are known as natural numbers. We are going to find sum of n natural numbers.
In other words, you can say we are going to find result of 1+2+3+4…..+n .

In this post, we will find sum of natural numbers using

  • For Loop
  • While Loop
  • Formula: We can find sum of n natural numbers using below formula –
    Sum of n natural numbers = (n * (n+1)) / 2,
    Where, n is count of natural numbers up-to which you want to find sum.
    

1. Find Sum of Natural Numbers (using for loop)

Sourcecode –

import java.util.*

fun main() {

    val read = Scanner(System.`in`)

    println("Enter maximum number:")
    val numb = read.nextInt()

    var sum = 0

    for(curNum in 1..numb) {
        sum += curNum
    }

    println("Sum of $numb natural number is $sum")
}

When you run the program, output will be

Enter maximum number:
20
Sum of 20 natural number is 210

Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.

read.nextIn() means read anything entered by user before space or line break from standard input – Keyboard.
Input read by scanner is then stored in variable year

We are using for loop to find sum of natural numbers in kotlin. In for loop, we are running loop from 1 to maximum number i.e. numb.

Let’s assume maximum number is 3. So,

For i = 1,
sum += curNum
=> sum = sum + curNum
=> sum = 0 + 1
=> sum = 1

For i = 2,
=> sum = 1 + 2
=> sum = 3

For i = 3,
=> sum = 3 + 3
=> sum = 6

So, sum of 3 natural number is 6.

Finally, we print result using println() method.

2. Find Sum of Natural Numbers using while block

We have already seen sum of natural numbers using for loop. Now, we will find sum of natural numbers using while loop in Kotlin.

import java.util.*

fun main() {

    val read = Scanner(System.`in`)

    println("Enter maximum number:")
    val numb = read.nextInt()

    var sum = 0
    var i = 1

    while (i <= numb) {
        sum += i
        i++
    }

    println("Sum of $numb natural number is $sum")
}

When you run the program, output will be

Enter maximum number:
30
Sum of 30 natural number is 465

Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.

read.nextIn() means read anything entered by user before space or line break from standard input – Keyboard.
Input read by scanner is then stored in variable year

We are using while loop to find sum of natural numbers instead of for loop. It works similar as for loop.

Let’s assume maximum number is 3. So,

While i = 1, (i <= numb) means (1 < 3). It's true. So, block inside while loop will be executed. sum += i
=> sum = sum + i
=> sum = 0 + 1
=> sum = 1

Also, i++ means i = i + 1. Value of i will be increased by 1. So, i = 2.

While i = 2, (2 <= 3). It's true. So, block inside while loop will be executed. => sum = 1 + 2
=> sum = 3

Also, value of i will be increased by 1. So, i = 3.

While i = 3, (3 <= 3). It's true. So, block inside while loop will be executed. => sum = 3 + 3
=> sum = 6

Also, value of i will be increased by 1. So, i = 4.

While i = 4, (4 <= 3). It's false. So, block inside while loop won't be executed. So, sum of 3 natural number is 6. Finally, we print result using println() method.

3. Sum of Natural Numbers using formula

We have already seen sum of natural numbers using for loop and while loop.
Now, we will find sum of natural numbers using formula.

As we have already discussed,

Formula to find sum = (n * (n+1)) / 2
Where, n is count of natural numbers.

Sourcecode –

import java.util.*

fun main() {

    val read = Scanner(System.`in`)

    println("Enter maximum number:")
    val numb = read.nextInt()

    val sum = (numb * (numb + 1)) / 2

    println("Sum of $numb natural number is $sum")
}

When you run the program, output will be

Enter maximum number:
30
Sum of 30 natural number is 465

Here, we used formula to find sum of n natural numbers. Remaining things are same as explained above.

Thus, we went through Kotlin program to find sum of n natural numbers.

Leave a Reply