Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SEOFriendlyImages has a deprecated constructor in /home/tutorig3/public_html/wp-content/plugins/seo-image/seo-friendly-images.class.php on line 2

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/tutorig3/public_html/wp-content/themes/mts_interactive/options/php-po/php-po.php on line 185
Kotlin Program to Print Armstrong Number Between Interval - Tutorialwing

Kotlin Program to Print Armstrong Number Between Interval

Write a Kotlin Program to Print Armstrong Number Between Interval a and b, given a < b.

What is Armstrong Number ?

A positive integer (x) of n digits is called armstrong number of order n (number of digits in x) if

abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....

Note that abcd… is number x where a, b, c, d ….etc. are it’s digits. n is number of digits in x.

For example,

a = 1 and b = 5

1 2 3 4 5
a = 6 and b = 200

6 7 8 9 153

1. Program to Print Armstrong Number Between Interval

Sourcecode –

import java.util.*
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.pow
import kotlin.math.roundToInt

fun main() {
    val read = Scanner(System.`in`)

    println("Enter a:")
    val a = read.nextInt()

    println("Enter b:")
    val b = read.nextInt()

    if(a >= b) {
        println("Invalid interval")

    println("Armstrong number between $a and $b: ")
    for(i in a..b) {
        if(checkArmstrong((i))) {
            print("$i ")

private fun checkArmstrong(number: Int): Boolean {

    // Get the count of digits in number
    val n = floor(log10(number.toDouble()) + 1).roundToInt()

    var x = number

    // Find sum by power last digit in n to count.
    var sum = 0
    while(x > 0) {
        val digit = x % 10
        sum = (sum + (digit.toDouble().pow(n))).roundToInt()
        x /= 10

    // Return true if sum is equal to number
    return sum == number

When you run the program, output will be –

Enter a:
Enter b:
Armstrong number between 1 and 5000:
1 2 3 4 5 6 7 8 9 153 370 371 407 1634

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.

Read the interval (a and b) entered by user and store it in variable a and b.

Run a loop from a to b.
Check for each value (i) if it is an armstrong number. If yes, print the value.

Thus, we went through Kotlin Program to Print Armstrong Number between interval a and b.