Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 03 Jan, 2022
Improve Article
Save Article
Like Article

What is an Automorphic number? A number is said to be Automorphic if its square ends in the same digits as the number itself. 


Input  : N = 76 
Output : Automorphic
Explanation: As 76*76 = 5776

Input  : N = 25
Output : Automorphic
As 25*25 = 625

Input : N = 7
Output : Not Automorphic
As 7*7 = 49

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 Kotlin 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="Automorphic Number or Not"
        app:layout_constraintVertical_bias="0.491" />
        android:text="Enter a number to check Automorphic 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.R.bool
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
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
            if (isAutomorphic(num))
                Toast.makeText(this, "Automorphic Number", Toast.LENGTH_SHORT).show()
                Toast.makeText(this, "Not an Automorphic Number", Toast.LENGTH_SHORT).show()
    fun isAutomorphic(N: Int): Boolean {
        // Store the square
        var N = N
        var sq = N * N
        // Start Comparing digits
        while (N > 0) {
            // Return false, if any digit of N doesn't
            // match with its square's digits from last
            if (N % 10 != sq % 10) return false
            // Reduce N and square
            N /= 10
            sq /= 10
        return true


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!