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.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
25
Sample output
Automorphic number
COMPLETE C PROGRAM
Complete C implementation
#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;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Automorphic number
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Square the number and compare its last d digits with the original d-digit number.
- 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.
