Skip to content
Related Articles

Related Articles

Build an Android App to Check a Number is Armstrong or Not

Improve Article
Save Article
  • Last Updated : 08 Oct, 2021
Improve Article
Save Article

What is Armstrong’s number? A number is said to be Armstrong if the sum of its digits is raised to the power of the total number of digits in the number. Given a number x, determine whether the given number is Armstrong’s number or not. A positive integer of n digits is called an Armstrong number of order n (order is the number of digits) if. 

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


153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153

120 is not an Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9

1253 is not an Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723

1634 is an Armstrong Number
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        android:text="Armstrong Number or Not"
        app:layout_constraintVertical_bias="0.491" />
        android:text="Enter a number to check Armstrong Number"
        app:layout_constraintVertical_bias="0.4" />
        android:hint="ENTER THE NUMBER"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
        app:layout_constraintVertical_bias="0.113" />

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. Here we will bind the views and write the logic of the app. 


import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import java.lang.Math.pow
import kotlin.math.pow
class MainActivity : AppCompatActivity() {
  lateinit var btnCheck : Button
  lateinit var etNum : EditText
    override fun onCreate(savedInstanceState: Bundle?) {
        // Binding the views
        btnCheck = findViewById(
        etNum = findViewById(
        btnCheck.setOnClickListener {
            val n = etNum.text.toString().length
            val num = etNum.text.toString().toInt()
            var temp = num
            var result = 0
            while (temp != 0){
                val remainder = temp % 10
                result += (remainder.toDouble().pow(n.toDouble())).toInt()
                temp /= 10
            if (result == num)
                Toast.makeText(this,"Armstrong Number",Toast.LENGTH_SHORT).show()
                Toast.makeText(this,"Not an Armstrong Number",Toast.LENGTH_SHORT).show()


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!