Tri-Automorphic Number in JAVA

Tri-Automorphic Number

Tri-Automorphic Number: A number is called Tri-Automorphic number if the last digits of 3 times the square of the number is equal to the number itself.

Or

A number ‘n’ is called Tri-Automorphic if ‘3n2 ends in ‘n’.


Example :-
Input      : 5

Sqaure    : 3 X (5 X 5) = 75

Output    : It is a Tri-Automorphic Number



Input      : 667

Sqaure    : 3 X 667 X 667 = 1334667

Output    : It is a Tri-Automorphic Number

 

Input      : 12

Square    : 3 X 12 X 12 = 432

Output    : It is not a Tri-Automorphic Number

Algorithm and Program given below :-

Algorithm :-

  • Input a number
  • Store the input number in another variable say ‘copy’
  • Store the ‘3n2’ of the input number in a variable
  • Now using while loop check the no. of digits in input number
  • Divide the ‘3n2’ of the input number by 10 with power equal to number of digits present in Input Number.
  • This will give a number having no. of digits same as of input no.
  • Compare this new number with input number
  • If it is equal print “It is a Tri-Automorphic Number”
  • Else “It is not a Tri-Automorphic Number”
  • Compile and Run the Program.

 

Program :-

import java.io.*;
import java.util.*;
class Tri_Automorphic
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n, sq, copy, c=0;
        System.out.println("Enter Number");
        n= in.nextInt();// Enter number from user
        sq=3*(n*n);// squaring the input number
        copy=n;//assigning entered number in copy variable
        while(copy>0)
        {
            c++;
            copy=copy/10;//taking quotient of the number
        }
        int end = sq%(int) Math.pow(10,c);//checking the digits of the number
        if(n==end)// comparing digits with original number
        {
            System.out.println("It is a Tri-Automorphic Number : "+n);
        }
        else
        {
            System.out.println("It is not a Tri-Automorphic Number : "+n);
        }
    }// end of main method
}// end of class


Follow me on Instagram
All the Best :)
Keep Learning :)


Comments

  1. You're doing a wonderful job by posting the solutions of all these programs... the solutions are so easy.. Keep it up :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA