Home Show/Hide Menu
Bluej Programs
➜ Test-paper-IT-10
➜ Suggestion Paper, Class-12

bluej Programs

4.Write a program to enter a number and check whether it is a Trimorphic number or not.[If the cube of a number ends with that numbe itself ,then the number is called the Trimorphic number. Example:n = 5
cube of n = 125 which ends with 5.
Therefore 5 is a Trimorphic number.

import java.util.*;
class Trimorphic_number
{
    Scanner sc=new Scanner(System.in);
    int n,cube;
    void input()
    {
        System.out.println("Enter a number:");
        n=sc.nextInt();
        if(n<=0)
    {
    System.out.println("Wrong Input!!Enter positive number");
    System.exit(0);
    }
    cube=n*n*n;
	}
    void display()
    {
         System.out.println("The original number"+n);
         System.out.println("Cube of "+n+" is:"+cube);
        if(check(n)==true)
        System.out.println(n+" is a Trimorphic number");
        else
         System.out.println(n+" is not a Trimorphic number");
    }
    boolean check(int x)
    {
        int d=0;
        for(int i=n;i>0;i/=10)
        {
            d++;
        }
        if(n==cube%(int)Math.pow(10,d))
        return true;
        else
        return false;
    }
    void main()
    {
        Trimorphic_number ob=new Trimorphic_number();
        ob.input();
        ob.display();
    }
}


Output:
Enter a number:
25
The original number25
Cube of 25 is:15625
25 is a Trimorphic number


 
ADVERTISEMENT