-1

iv made a method to check if a given number is symmetric or not.

-the method is based on the idea that the given number is an array .

-i gave the methood 4 deifferent parameters- numberLength , middle , point1,point2(both pointing at a certain digit in the number(as an array))

-although wrote the code correctly , and it works when im initializing a specific array , i dont know how to use the method for a given number .

How can i set this Method - with parameter (int number) , so i can chek on the given number.

Thank you A lot

update** i added the code :

public boolean isSymmetric(int num){           
        int digits;
        int[] number;
        number = new int[num];
        int length = number.length;
        int mid;
        if(length%2 == 0) // 
        {
            mid = length/2;
        }else {
            mid = length/2+1;
        }
        int pointer1 =0;
        int pointer2 = mid;
        while(pointer1<mid && pointer2 < length)
        {
            if(number[pointer1] == number[pointer2])
            {
                pointer1=pointer1+1;
                pointer2=pointer2+1;
            }
            else  
                System.out.println("number is not symmetric");
                return false;
        }
        System.out.println("number is symmetric");
        return true;
    }
OriG
  • 1
  • 2
  • 1
    Can you add some samples of expected input and output? Also share what code you have tried. Else see if this helps: https://stackoverflow.com/questions/15299312/checking-if-array-is-symmetric – Sid Apr 10 '22 at 10:18
  • Having provided an answer I realized that I may not understand what you mean by symmetric. is `123123` consider symmetric or is it `1234321`? If it is the former I might point out that numbers with an odd number of digits can't be symmetric. – WJS Apr 11 '22 at 21:15

2 Answers2

0

The easiest way to check if number symmetric or not is to map it to String::class, just like this:

// Your input number
Integer maybeSymmetricNumber = 12321;

String str = String.valueOf(maybeSymmetricNumber), reverseStr = "";

int strLength = str.length();

for (int i = (strLength - 1); i >=0; --i) {
  reverseStr = reverseStr + str.charAt(i);
}

if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
  System.out.println(str + " is a symmetric number.");
}
else {
  System.out.println(str + " is not a symmetric number.");
}
Yahor Barkouski
  • 1,361
  • 1
  • 5
  • 21
0

First, here is a method to convert your number to an array of ints.

  • it works by using the Math.log10 to compute the exponent of 10 for that number.
    • e.g. Math.log10(1234) = 3.xxx. So convert to an int and add 1 to get 4.
    • e.g. Math.log10(1000) = 3 so do the same thing.
  • then use that number to create an array of proper size.
  • and use the remainder(%) and division(/) operators to populate it.
  • then return the array.

public static int[] toArray(int number) {
    number = Math.abs(number);
    int[] arr = new int[(int) Math.log10(number)+1];
    int i = 0;
    while (number > 0) {
        arr[i++] = number%10;
        number/=10;
    }
    return arr;
}

Unfortunately, your method doesn't work properly as it always returns false. This is because of missing {} around the following.

else  
     System.out.println("number is not symmetric");
     return false;

But it still doesn't process all values properly. Try 1234321 and 112211. They both return false but are symmetric. This is because pointer1 starts at 0 and pointer2 starts at mid and they are both incremented by 1.

I suggest you start pointer2 at length-1, going from outside in toward mid. This should then solve the issue (with a little debugging of course).

WJS
  • 36,363
  • 4
  • 24
  • 39
  • Thank you all. as said .i found a Method to know if a number is symmetric or not . my issue is i dont kniw how to use given number as an array . – OriG Apr 11 '22 at 06:12
  • @OriG I modified my answer and found a few issues. Let me know if you have questions. – WJS Apr 11 '22 at 15:55