亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Logic Behind Palindrome in Java
How to Test Palindrome using Various Methods?
1. Program to check palindrome number using for loop
2. Program to check palindrome number using While loop
3. Program to check palindrome number using library method (for strings)
Conclusion
Home Java javaTutorial Palindrome in Java

Palindrome in Java

Aug 30, 2024 pm 04:26 PM
java

String or a number is said to be a palindrome if it remains the same even after it is reversed. For example, ‘MADAM’ is a palindrome string since it is spelled ‘MADAM’ even if it is reversed. But in the case of ‘LUCKY’, this string is not palindrome as it is ‘YKCUL’ when it is reversed. Some of the palindrome numbers are 365563, 48984, 12321, 171, 88, 90009, 343, and some of the palindrome strings are MADAM, MALAYALAM, LOL, DAD, MOM, C++&++C, etc. Let us see the logic and implementation of palindrome in the following sections. In this topic, we are going to learn about?Palindrome in Java.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Logic Behind Palindrome in Java

In order to check whether a number is a palindrome, the following algorithm can be used.

  • Take an input string or number that has to be checked whether it is a palindrome or not.

For example, let us take the number 353 as input.

  • Take the input number and copy it into a temp variable

353-> temp

  • Reverse it using for, while or any method of your choice.

Reversednumber: rev=353

  • Compare the input number and reversed number.

If they are the same, then the number is said to be a palindrome number.

Else, the number is not a palindrome number.

i.e.

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

How to Test Palindrome using Various Methods?

There are several methods in order to check whether the given input number is a palindrome or not.

  • For Loop
  • While Loop
  • Library Method(for strings)

Let us look into each of them in detail:

1. Program to check palindrome number using for loop

Code:

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

Output 1:

Palindrome in Java

Here, as 353 is the same when reversed, it is considered as a palindrome.

Output 2:

Palindrome in Java

Here, as 234 remains not the same when reversed, it is not considered a palindrome.

2. Program to check palindrome number using While loop

Code:

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

Output 1:

Palindrome in Java

Output 2:

Palindrome in Java

3. Program to check palindrome number using library method (for strings)

Code:

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

Output:

Palindrome in Java

Here, the input string is passed in the program itself.

To check whether a string is a palindrome, the following program is also used.

Code:

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

Output:

Palindrome in Java

Conclusion

A number is said to be palindrome if it remains the same even when it is reversed. A palindrome can be checked in strings also. Some of the palindrome numbers and strings are MOM, MALAYALAM, DAD, LOL, 232, 1331, etc. In this document, several aspects of Palindrome are covered, such as algorithm, methods, implementation, etc.

The above is the detailed content of Palindrome in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JavaScript to determine whether two arrays are equal? How to use JavaScript to determine whether two arrays are equal? May 23, 2025 pm 10:51 PM

In JavaScript, you need to use a custom function to determine whether two arrays are equal, because there is no built-in method. 1) Basic implementation is to compare lengths and elements, but cannot process objects and arrays. 2) Recursive depth comparison can handle nested structures, but requires special treatment of NaN. 3) Special types such as functions and dates need to be considered, and further optimization and testing are required.

How to correctly handle this pointing in a closure? How to correctly handle this pointing in a closure? May 21, 2025 pm 09:15 PM

The methods to correctly handle this pointing in JavaScript closures include: 1. Use arrow functions, 2. Use bind methods, 3. Use variables to save this. These methods ensure that this intrinsic function correctly points to the context of the external function.

How to implement data encryption with JavaScript? How to implement data encryption with JavaScript? May 23, 2025 pm 11:12 PM

Using JavaScript to implement data encryption can use the Crypto-JS library. 1. Install and introduce the Crypto-JS library. 2. Use the AES algorithm for encryption and decryption to ensure that the same key is used. 3. Pay attention to the secure storage and transmission of keys. It is recommended to use CBC mode and environment variables to store keys. 4. Consider using WebWorkers when you need high performance. 5. When processing non-ASCII characters, you need to specify the encoding method.

What are the four categories of Java? Description of Java Basic Type System Classification What are the four categories of Java? Description of Java Basic Type System Classification May 20, 2025 pm 08:27 PM

Java's four basic type systems include integer types, floating point types, character types and boolean types. 1. Integer types (byte, short, int, long) are used to store numerical values ??without decimals. Choosing the appropriate type can optimize memory and performance. 2. Float type (float, double) is used for decimal values. Pay attention to accuracy issues. If necessary, BigDecimal is used. 3. Character type (char) is based on Unicode and is suitable for single characters, but String may be required in international applications. 4. Boolean types are used for true and false values, simplifying logical judgments and improving code readability.

How to copy and paste layer styles in PS? How to copy and paste layer styles in PS? May 16, 2025 am 06:00 AM

Copying and pasting layer styles in Photoshop is a key trick to improve productivity. Let's dive into how to do it, and the various details and techniques you may encounter in the process. When we talk about copying and pasting layer styles in Photoshop, the first thing we need to understand is that layer styles refer to effects applied to layers, such as shadows, glows, bevels, and reliefs. Mastering this feature not only saves time, but also ensures consistent design. To copy the style of a layer, right-click the layer you want to copy and select Copy Layer Style. This will copy all the styles of the layer into the clipboard. Next, select the target layer you want to apply these styles, right-click it, and select Paste

What does u mean in c language? Unsigned modification of u in c language What does u mean in c language? Unsigned modification of u in c language May 16, 2025 pm 02:06 PM

u is used in C language to declare unsigned integer constants. 1. The u suffix represents an unsigned integer, such as 10u. 2. The range of unsigned integers starts from 0 and does not contain negative numbers. They are suitable for large-range positive numbers and bit operations. 3. Pay attention to overflow and negative number processing issues when using unsigned integers.

The difference between programming in Java and other languages ??Analysis of the advantages of cross-platform features of Java The difference between programming in Java and other languages ??Analysis of the advantages of cross-platform features of Java May 20, 2025 pm 08:21 PM

The main difference between Java and other programming languages ??is its cross-platform feature of "writing at once, running everywhere". 1. The syntax of Java is close to C, but it removes pointer operations that are prone to errors, making it suitable for large enterprise applications. 2. Compared with Python, Java has more advantages in performance and large-scale data processing. The cross-platform advantage of Java stems from the Java virtual machine (JVM), which can run the same bytecode on different platforms, simplifying development and deployment, but be careful to avoid using platform-specific APIs to maintain cross-platformity.

After installing Nginx, the configuration file path and initial settings After installing Nginx, the configuration file path and initial settings May 16, 2025 pm 10:54 PM

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

See all articles