Open In App

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

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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) + .... 

Example: 

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




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/teal_200">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Armstrong Number or Not"
        android:textColor="@color/black"
        android:textSize="26sp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.491" />
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter a number to check Armstrong Number"
        android:textColor="@color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4" />
  
    <EditText
        android:id="@+id/etNum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="ENTER THE NUMBER"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
  
    <Button
        android:id="@+id/btnCheck"
        android:layout_width="149dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_marginTop="16dp"
        android:backgroundTint="@color/black"
        android:text="Check"
        android:textColor="@color/teal_700"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/etNum"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="@+id/etNum"
        app:layout_constraintTop_toBottomOf="@+id/etNum"
        app:layout_constraintVertical_bias="0.113" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


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. 

Kotlin




import androidx.appcompat.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportActionBar?.hide()
          
        // Binding the views
        btnCheck = findViewById(R.id.btnCheck)
        etNum = findViewById(R.id.etNum)
  
        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()
            else
                Toast.makeText(this,"Not an Armstrong Number",Toast.LENGTH_SHORT).show()
        }
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads