Table of contents
1.
Introduction
2.
Understanding Automorphic Numbers
3.
Syntax 
3.1.
Example 
3.2.
C
3.3.
Explanation of the Code
3.4.
Code Output
4.
Key Features of Automorphic Number Check
5.
Frequently Asked Questions
5.1.
How can I check if a number is automorphic in C?
5.2.
Are automorphic numbers only positive integers?
5.3.
Can this method be used for large numbers?
6.
Conclusion
Last Updated: Aug 14, 2024
Easy

Automorphic Numbers in C

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In mathematics, an automorphic number is a number whose square ends in the same digits as the number itself. 

For example, 5 is an automorphic number because 5^2 = 25 and 25 ends in 5. Similarly, 6 is an automorphic number because 6^2 = 36, and 36 ends in 6.

Automorphic Numbers in C

In C, checking if a number is automorphic involves few simple steps. This article will help you understand automorphic numbers and how to implement a program to check them in C.

Understanding Automorphic Numbers

An automorphic number, also known as a circular number, consists of a special property. When squared, the number appears as the last digits of the result. Here’s how you can determine if a number is automorphic:

  1. Square the number.
     
  2. Check if the square ends with the original number.
     

Let’s explore how to implement this concept in C.

Syntax 

To check if a number is automorphic in C, follow these steps:

  1. Square the given number.
     
  2. Find the number of digits in the original number.
     
  3. Extract the last digits from the squared number that match the number of digits.
     
  4. Compare these last digits with the original number.

Example 

Here’s a simple C program to check if a number is automorphic:

  • C

C

#include <stdio.h>

#include <math.h>

int isAutomorphic(int num) {

   int square = num * num; // Step 1: Calculate the square of the number

   int digits = (int)log10(num) + 1; // Step 2: Calculate number of digits

   int lastDigits = square % (int)pow(10, digits); // Step 3: Extract last digits

   // Step 4: Compare last digits with the original number

   return (lastDigits == num);

}


int main() {

   int number;

   // Get user input

   printf("Enter a number: ");

   scanf("%d", &number);

   // Check if the number is automorphic

   if (isAutomorphic(number)) {

       printf("%d is an automorphic number.\n", number);

   } else {

       printf("%d is not an automorphic number.\n", number);

   }

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Explanation of the Code

  1. Function isAutomorphic(int num):
    • Square Calculation: num * num calculates the square of the number.
       
    • Digit Count: log10(num) + 1 finds the number of digits in the number.
       
    • Last Digits Extraction: square % (int)pow(10, digits) extracts the last digits of the squared number.
       
    • Comparison: The function returns 1 (true) if the last digits match the original number, otherwise 0 (false).
       
  2. main() Function:
    • Prompts the user to enter a number.
       
    • Calls isAutomorphic() to check if the number is automorphic.
       
    • Displays the result based on the function’s return value.

Code Output

If you run the program with the input 6, the output will be:

Enter a number: 6
6 is an automorphic number.

Key Features of Automorphic Number Check

  • Simple Calculation: The check involves basic arithmetic operations and modulo operations.
     
  • Efficiency: The time complexity is constant, O(1)O(1)O(1), because it involves only a few arithmetic operations regardless of the size of the input number.
     
  • Flexibility: The program can be used to check any positive integer.

Frequently Asked Questions

How can I check if a number is automorphic in C?

You can check if a number is automorphic by squaring the number, extracting the last digits of the squared result, and comparing them to the original number. This can be done using the modulo operation.

Are automorphic numbers only positive integers?

Yes, automorphic numbers are typically considered for positive integers. However, similar concepts can be applied to other types of numbers with appropriate adjustments.

Can this method be used for large numbers?

Yes, this method works for large numbers, but keep in mind that the integer data type in C has limits. For very large numbers, you might need to use larger data types or special libraries for handling big integers.

Conclusion

Automorphic numbers in C are an interesting concept in number theory with practical applications. By understanding and implementing the check for automorphic numbers in C, you gain a deeper insight into both mathematical properties and programming techniques. The provided C program offers a simple and efficient way to determine if a number is automorphic.

You can also check out our other blogs on Code360.

Live masterclass