Wprowadzenie do Palindrome w Javie

Mówi się, że ciąg lub liczba jest palindromem, jeśli pozostaje taki sam, nawet po odwróceniu. Na przykład „MADAM” jest ciągiem palindromu, ponieważ jest napisany „MADAM”, nawet jeśli jest odwrócony. Ale w przypadku „LUCKY” ten ciąg nie jest palindromem, ponieważ jest „YKCUL”, gdy jest odwrócony. Niektóre numery palindromów to 365563, 48984, 12321, 171, 88, 90009, 343, a niektóre ciągi palindromów to MADAM, MALAYALAM, LOL, DAD, MOM, C ++ i ++ C itd . Zobaczmy logikę i implementację palindromu w poniższych sekcjach. W tym temacie poznamy Palindrom w Javie.

Logika stojąca za Palindromem w Javie

Aby sprawdzić, czy liczba jest palindromem, można zastosować następujący algorytm.

  • Weź ciąg wejściowy lub liczbę, którą należy sprawdzić, czy jest to palindrom, czy nie.

Weźmy na przykład liczbę 353 jako dane wejściowe.

  • Weź numer wejściowy i skopiuj go do zmiennej temp

353-> temp

  • Odwróć to za pomocą for, while lub dowolnej wybranej metody.

Reversednumber: rev=353

  • Porównaj numer wejściowy i numer odwrócony.

Jeśli są takie same, mówi się, że liczba jest liczbą palindromową.

W przeciwnym razie liczba nie jest liczbą palindromową.

to znaczy

If(inputnum==rev)
( then palindrome )
Else not palindrome

Jak przetestować palindrom przy użyciu różnych metod?

Istnieje kilka metod sprawdzania, czy podany numer wejściowy jest palindromem, czy nie.

  1. Dla pętli
  2. Podczas gdy pętla
  3. Metoda biblioteczna (dla łańcuchów)

Przyjrzyjmy się szczegółowo każdemu z nich.

1. Program do sprawdzania liczby palindromów za pomocą pętli for

//Java program to check whether a String is a Palindrome or not using For Loop
import java.util.*;
public class PalindromeNumberExample (
//main method
public static void main(String() args) (
int r=0 ; //reversed Integer
int rem, num; //remainder and original number
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
for( ;num != 0; num /= 10 )
(
rem = num % 10; // find the modulus of the number when divided by 10
r = r * 10 + rem;
)
//check whether the original and reversed numbers are equal
if (temp == r)
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
)
else
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
)
)
)

Przykładowy wynik 1:

Tutaj, ponieważ 353 jest taki sam po odwróceniu, jest uważany za palindrom.

Przykładowy wynik 2:

Tutaj, ponieważ 234 nie jest taki sam po odwróceniu, nie jest uważany za palindrom.

2. Program do sprawdzania liczby palindromów za pomocą pętli While

//Java program to check whether a number is a Palindrome or not using While Loop
import java.util.*;
public class PalindromeNumberExample (
public static void main(String() args) (
int r=0, rem, num;
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
while( num != 0 )
(
rem= num % 10;
r= r * 10 + rem;
num=num/10;
)
//check whether the original and reversed numbers are equal
if (temp == r)
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
)
else
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
)
)
)

Przykładowy wynik 1:

Przykładowy wynik 2:

3. Program do sprawdzania numeru palindromu za pomocą metody bibliotecznej (dla ciągów znaków)

//Java program to check whether a String is a Palindrome or not using Library method
import java.util.*;
public class PalindromeNumberExample (
//Function to check whether the string is palindrome or not
public static void PalindromeCheck(String str)
(
// reverse the input String
String rev = new StringBuffer(str).reverse().toString();
// checks whether the string is palindrome or not
if (str.equals(rev))
(
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ str +" is a palindrome");
)
else
(
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ str +" is not a palindrome");
)
)
public static void main (String() args)
(
PalindromeCheck("MALAYALAM");
)
)

Przykładowe dane wyjściowe:

Tutaj ciąg wejściowy jest przekazywany do samego programu.

Aby sprawdzić, czy ciąg znaków jest palindromem, używany jest również następujący program.

//Java program to check whether a String is a Palindrome or not
import java.util.*;
public class PalindromeNumberExample (
public static void main(String args())
(
String st, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string that has to be checked:");
st = sc.nextLine();
int len = st.length(); //length of the string
for ( int i = len- 1; i >= 0; i-- )
rev = rev + st.charAt(i);
if (st.equals(rev))
(
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ st +" is a palindrome");
)
else
(
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ st +" is not a palindrome");
)
)
)

Przykładowe dane wyjściowe:

Wniosek

Mówi się, że liczba jest palindromem, jeśli pozostaje taka sama, nawet gdy jest odwrócona. Palindrom można również sprawdzić w ciągach. Niektóre liczby i łańcuchy palindromu to MOM, MALAYALAM, DAD, LOL, 232, 1331 itd. W tym dokumencie omówiono kilka aspektów Palindrome, takich jak algorytm, metody, implementacja itp.

Polecane artykuły

To jest przewodnik po Palindrome w Javie. Tutaj omawiamy, jak testować palindrom przy użyciu różnych metod z przykładowym wyjściem. Możesz także zapoznać się z następującymi artykułami, aby dowiedzieć się więcej -

  1. Pierwiastek kwadratowy w Javie
  2. Odwrotna liczba w Javie
  3. StringBuffer w Javie
  4. CardLayout w Javie
  5. Przegląd Palindrome w C #
  6. Odwróć w JavaScript
  7. Narzędzia wdrażania Java
  8. Palindrom w C ++
  9. Pierwiastek kwadratowy w PHP
  10. Praca i 3 najlepsze metody wyliczania w C #