Úvod do Palindrómu v Jave

Reťazec alebo číslo sa považuje za palindróm, ak zostáva rovnaký aj po jeho obrátení. Napríklad „MADAM“ je palindrómový reťazec, pretože je napísaný ako „MADAM“, aj keď je obrátený. V prípade výrazu „LUCKY“ však tento reťazec nie je palindróm, pretože je „YKCUL“, keď je obrátený. Niektoré z čísel palindrómu sú 365563, 48984, 12321, 171, 88, 90009, 343 a niektoré z palindrómových reťazcov sú MADAM, MALAYALAM, LOL, DAD, MOM, C ++ a ++ C, atď . Pozrime sa na logiku a implementáciu palindrómu v nasledujúcich častiach. V tejto téme sa dozvieme o Palindrome v Jave.

Logika za Palindrome v Jave

Na overenie, či je číslo palindrómom, sa môže použiť nasledujúci algoritmus.

  • Vezmite si vstupný reťazec alebo číslo, ktoré sa musí skontrolovať, či je to palindróm alebo nie.

Napríklad vezmime číslo 353 ako vstup.

  • Vezmite vstupné číslo a skopírujte ho do dočasnej premennej

353-> temp

  • Reverzujte to pomocou metódy pre, while alebo inou metódou podľa vášho výberu.

Reversednumber: rev=353

  • Porovnajte vstupné číslo a obrátené číslo.

Ak sú rovnaké, potom sa toto číslo považuje za palindrómové číslo.

Inak to číslo nie je palindrómové číslo.

tj

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

Ako testovať palindróm pomocou rôznych metód?

Existuje niekoľko metód, ako skontrolovať, či je dané vstupné číslo palindróm alebo nie.

  1. Pre slučku
  2. Zatiaľ čo slučka
  3. Metóda knižnice (pre reťazce)

Pozrime sa podrobne na každú z nich.

1. Program pre kontrolu čísla palindrómu pomocou slučky

//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");
)
)
)

Vzorový výstup 1:

Tu, keďže 353 je pri obrátení rovnaký, považuje sa za palindróm.

Vzorový výstup 2:

Tu, pretože 234 zostáva pri obrátení nezmenený, nepovažuje sa to za palindróm.

2. Programom skontrolujte číslo palindrómu pomocou slučky 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");
)
)
)

Vzorový výstup 1:

Vzorový výstup 2:

3. Program na kontrolu čísla palindrómu pomocou metódy knižnice (pre reťazce)

//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");
)
)

Vzorový výstup:

Tu vstupný reťazec prechádza samotným programom.

Na kontrolu, či je reťazec palindrómom, sa použije aj nasledujúci 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");
)
)
)

Vzorový výstup:

záver

Hovorí sa, že číslo je palindróm, ak zostáva rovnaké, aj keď je obrátené. Palindróm je možné skontrolovať aj v reťazcoch. Niektoré z palindrómových čísiel a reťazcov sú MOM, MALAYALAM, DAD, LOL, 232, 1331 atď. V tomto dokumente je zahrnutých niekoľko aspektov Palindrómu, ako sú algoritmus, metódy, implementácia atď.

Odporúčané články

Toto je príručka pre program Palindrome v Jave. Tu diskutujeme o tom, ako otestovať palindróm pomocou rôznych metód s výstupom vzorky. Ďalšie informácie nájdete aj v nasledujúcich článkoch -

  1. Square Root v Jave
  2. Reverzné číslo v jazyku Java
  3. StringBuffer v Jave
  4. CardLayout v Jave
  5. Prehľad palindrómu v C #
  6. Obrátiť sa na JavaScript
  7. Java Deployment Tools
  8. Palindróm v C ++
  9. Štvorcový koreň v PHP
  10. Pracovné a najlepšie 3 metódy výčtu v C #

Kategórie: