-5

Definition: A number is called an Automorphic number if and only if its square ends in the same digits as the number itself.

This is what I wrote for the code:

import java.util.*;
public class AutomorphicOrNot
{
    public static void main(String [] args)
    {
        System.out.println("Enter a number:");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int n;
        int i;
        int t=x^2;
        
        for(i=1;i<t;i++)
        {
            t=t%10;
        }
        n=i;
        if((t%(Math.pow(10,n)))==x)
        {
           System.out.println("It is an automorphic number");
        }
        else
        {
           System.out.println("It is not an automorphic number");
        }
    }
}

Mathematically, the code looks correct to me. However, I got the wrong output for the same. For example, when I enter 25, I get: It is not an automorphic number. Similarly there is a discrepancy for other entries as well.

I have just recently begun to learn coding. I hope you could help me to figure out the error. Thank you.

  • `x^2` is not `x squared`. It is `x XOR 2`. https://stackoverflow.com/questions/10217390/what-does-caret-mean-in-java You want `Math.pow(x, 2)` – Liftoff Jun 24 '23 at 10:23
  • Hello, thank you for your response, I tried "Math.pow(x,2)", however during compilation, the message: "incompatible types, possible lossy conversion from double to int" shows up. I have not used double in my code at all. Why could this be? Further, I also tried using x*x instead, but the output still isn't correct. – apocalyptic Jun 24 '23 at 10:28
  • `Math.pow` returns a double. You'll have to explicitly cast to an int. `int t = (int)(Math.pow(x, 2))` – Liftoff Jun 24 '23 at 10:29
  • True, thank you. There are no further compilation errors, however the output displayed is still incorrect. I am still getting "not automorphic" for some numbers which are in fact automorphic. – apocalyptic Jun 24 '23 at 10:35
  • You're using floating point numbers (`Math.pow(...)` returns a double), and testing for equality with doubles is inexact. – Hovercraft Full Of Eels Jun 24 '23 at 10:56
  • Floating point numbers can represent integers exactly up to a point, (2^23 for float, and 2^52 for double). Once you exceed the number of mantissa bits, you start to lose precision. Since OP is just using it for integer exponentiation, as long as the result is <= 2^52, it should be fine. – Liftoff Jun 24 '23 at 11:02

1 Answers1

0

You have two errors.

1. ^ is the bitwise XOR operator.

It is not an exponent operator. You want Math.pow instead.

2. Your loop.

for(i=1;i<t;i++)
{
  t=t%10;
}

Your intention here is to determine how many digits are in your starting number, not your squared number. Instead, you should be dividing your starting number by 10 until the result is less than 10. You will also need to store a copy of the starting number so you can later compare that to the modulus of your squared number.

int x = sc.nextInt();
int i = 1;
int t = (int)(Math.pow(x, 2));
int n = x;

while(n >= 10)
{
  n /= 10;
  i++;
}

The final result looks like this, and does state that 25 is an automorphic number.

import java.util.*;
class HelloWorld {
    public static void main(String [] args)
    {
        System.out.println("Enter a number:");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int i = 1;
        int t = (int)(Math.pow(x, 2));
        int n = x;
        
        while(n >= 10)
        {
            n /= 10;
            i++;
        }

        if((t % (Math.pow(10,i))) == x)
        {
           System.out.println("It is an automorphic number");
        }
        else
        {
           System.out.println("It is not an automorphic number");
        }
    }
}
Liftoff
  • 24,717
  • 13
  • 66
  • 119