Program to Find the nth Automorphic number

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 find the nth Automorphic number.

Data requirement:-

   Input Data:- rangenumber

   Output Data:- rangenumber, letest

   Additional Data:- cnum, num1, flag, sqr 

Program in C

Here is the source code of the C Program to find the nth Automorphic Number.

Code:

#include <stdio.h>
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
  printf ("Enter the Nth value:");
  scanf ("%d", &rangenumber);

  while (c != rangenumber)
    {
      int num1=num;
       int sqr=num1*num1;
        int flag=0;
      while(num1>0)
    {
       if(num1%10 != sqr%10)
         {flag=-1;
           break;
         }
       num1=num1/10;
       sqr=sqr/10;
   }
     if(flag==0)
      {
          c++;
          letest=num;
      }
      num = num + 1;
    }
  printf ("%dth Automorphic number is %d", rangenumber, letest);
  return 0;
}

Input/Output:
Enter the Nth value:5
5th Automorphic number is 76

Program in C++

Here is the source code of the C++ Program to find the nth Automorphic Number.

Code:

//nth Automorphic number
#include <iostream>
using namespace std;
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
 cout<<"Enter the Nth value:";
  cin>>rangenumber;

  while (c != rangenumber)
    {
      int num1=num;
       int sqr=num1*num1;
        int flag=0;
      while(num1>0)
    {
       if(num1%10 != sqr%10)
         {flag=-1;
           break;
         }
       num1=num1/10;
       sqr=sqr/10;
   }
     if(flag==0)
      {
          c++;
          letest=num;
      }
      num = num + 1;
    }
  cout<<rangenumber<<"th Automorphic number is "<<letest;
  return 0;
}

Input/Output:
Enter the Nth value:7
7th Automorphic number is 625

Program in Java

Here is the source code of the Java Program to find the nth Automorphic Number.

Code:

import java.util.Scanner;
public class NthAutomorphic {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
 int  rangenumber, num = 1, c = 0, letest = 0;
  System.out.println("Enter Nth number:");
  rangenumber=cs.nextInt();

  while (c != rangenumber)
    {
      int num1=num;
       int sqr=num1*num1;
        int flag=0;
      while(num1>0)
    {
       if(num1%10 != sqr%10)
         {flag=-1;
           break;
         }
       num1=num1/10;
       sqr=sqr/10;
   }
     if(flag==0)
      {
          c++;
          letest=num;
      }
      num = num + 1;
    }
System.out.println(rangenumber+"th Automorphic number is "+letest);
cs.close();
}
}

Input/Output:
Enter Nth number:
4
4th Automorphic number is 25


Program in Python 

Here is the source code of the Python Program to find the nth Automorphic Number.

Code:

rangenumber=int(input("Enter an Nth Number:"))
c = 0
letest = 0
num = 1
while c != rangenumber:
    num1 = num
    sqr = num1 * num1
    flag = 0
    while num1>0:
        if num1%10 != sqr%10:
            flag = -1
            break
        num1 = num1 // 10
        sqr = sqr // 10

    if flag==0:
        c+=1
        letest = num

    num = num + 1
print(rangenumber,"th Automorphic number is ",letest)

Input/Output:
Enter an Nth Number:2
2 th Automorphic number is  5


More:-

C/C++/Java/Python Practice Question 

Post a Comment

0 Comments