Check a given number is an Automorphic number using recursion

A Number is an Automorphic Number when the square ends in the same digits as the number itself. 

Example:
               
               5*5=25 //Automorphic Number
               
               625*625=390625 //Automorphic Number
               

               7*7=49 //Not an Automorphic Number 

Problem statement:- Program to Check a given number is an Automorphic number or not using recursion.

Data requirement:-

   Input Data:- num

  Output Data:-
String Output

Program in C

Here is the source code of the C Program to Check a given number is an Automorphic number or not using recursion.

Code:

#include<stdio.h>
int check_AutomorphicNumber(int num)
{
    int sqr=num*num;
   if(num>0)
   {
       if(num%10 != sqr%10)
         {
           return -1;
         }
         else
            return 0;

       check_AutomorphicNumber(num/10);
       sqr=sqr/10;
   }
   return 0;
}
int main()
{
    int num;
    printf("Enter a number:");
    scanf("%d",&num);

   if(check_AutomorphicNumber(num)==0)
    printf("It is an Automorphic Number.");
   else
    printf("It is not an Automorphic Number.");
}

Input/Output:
Enter a number:5
It is an Automorphic Number.

Program in C++

Here is the source code of the C++ Program to Check a given number is an Automorphic number or not using recursion.

Code:

#include<iostream>
using namespace std;
int check_AutomorphicNumber(int num)
{
    int sqr=num*num;
   if(num>0)
   {
       if(num%10 != sqr%10)
         {
           return -1;
         }
         else
            return 0;

       check_AutomorphicNumber(num/10);
       sqr=sqr/10;
   }
   return 0;
}
int main()
{
    int num;
    cout<<"Enter a number:";
    cin>>num;

   if(check_AutomorphicNumber(num)==0)
    cout<<"It is an Automorphic Number.";
   else
    cout<<"It is not an Automorphic Number.";
}

Input/Output:
Enter a number:33
It is not an Automorphic Number.

Program in Java

Here is the source code of the Java Program to Check a given number is an Automorphic number or not using recursion.

Code:

import java.util.Scanner;
public class AutomorphicNumberOrNot {
static int check_AutomorphicNumber(int num)
{
    int sqr=num*num;
   if(num>0)
   {
       if(num%10 != sqr%10)
         {
           return -1;
         }
         else
        check_AutomorphicNumber(num/10);
            return 0;
   }
   return 0;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
    int num;
    System.out.print("Enter a number:");
    num=cs.nextInt();
    if(check_AutomorphicNumber(num)==0)
    System.out.print("It is an Automorphic Number.");
       else
       System.out.print("It is not an Automorphic Number.");
    cs.close();
}
}

Input/Output:
Enter a number:76
It is an Automorphic Number.

Program in Python

Here is the source code of the Python Program to Check a given number is an Automorphic number or not using recursion.

Code:

def check_AutomorphicNumber(num):
    sqr = num * num
    if (num > 0):
        if (num % 10 != sqr % 10):
            return -1
        else:
            check_AutomorphicNumber(num // 10)
            return 0
    return 0
num=int(input("Enter a number:"))
if (check_AutomorphicNumber(num) == 0):
    print("It is an Automorphic Number.")
else:
    print("It is not an Automorphic Number.")

Input/Output:

Post a Comment

0 Comments