Trimorphic Number in JAVA

Trimorphic Number

Trimorphic Number: A number is called Trimorphic Number if and only if its cube ends in the same digits as the number itself. 

Or 
A number ‘n’ is called Trimorphic if ‘n^3’ ends in ‘n’ 
Example :-
Input : 5 
Cube : 5 X 5 X 5 = 625 
Output : It is Trimorphic Number 
Input : 49 
Cube : 49 X 49 X 49 = 117649 
Output : It is a Trimorphic Number 
Input : 7 
Cube : 7 X 7 X 7 = 343 
Output : It is not a Trimorphic Number  

Algorithm and Program given below :-

Algorithm :-

  • Input a number
  • Store the input number in another variable say ‘copy’
  • Store the ‘n3’ of the input number in a variable
  • Now using while loop check the no. of digits in input number
  • Divide the ‘n3’ 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 Trimorphic Number”
  • Else “It is not a Trimorphic Number”
  • Compile and Run the Program.

 

Program :-

import java.io.*;
import java.util.*;
class Trimorphic
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n, cube, copy, c=0;
        System.out.println("Enter Number");
        n= in.nextInt();// Enter number from user
        cube=n*n*n;// Storing Cube of 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 = cube%(int) Math.pow(10,c);//checking the digits of the number
        if(n==end)// comparing digits with original number
        {
            System.out.println("It is Trimorphic : "+n);
        }
        else
        {
            System.out.println("It is not Trimorphic : "+n);
        }
    }// end of main method
}// end of class 


Video Link of this Program : https://youtu.be/3hj2Q3ol5ek

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

Comments

Post a Comment

Popular posts from this blog

Frequency of each digit of a number in Java

Tri-Automorphic Number in JAVA