C PROGRAMMING PROGRAM • LOOPS & NUMBER PROBLEMS

Check Whether a Number Is Automorphic

Learn how to check whether a number is automorphic using a clear C program.

BeginnerSquarePlace valueAutomorphic number

PROBLEM UNDERSTANDING

Input and expected output

Sample input
25
Sample output
Automorphic number

COMPLETE C PROGRAM

Complete C implementation

automorphic-number.c
Open in compiler
#include <stdio.h>

int main(void)
{
    long long number, square, divisor = 10;

    printf("Enter a non-negative integer: ");
    scanf("%lld", &number);
    if (number < 0) {
        printf("Use a non-negative integer.\n");
        return 0;
    }
    square = number * number;
    for (long long value = number; value >= 10; value /= 10) divisor *= 10;
    printf(square % divisor == number ? "Automorphic number\n" : "Not an automorphic number\n");
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Automorphic number
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Square the number and compare its last d digits with the original d-digit number.
  3. Display the computed result.

Square the number and compare its last d digits with the original d-digit number.

EFFICIENCY

Time and space complexity

Time complexity

O(d)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.