Java-program til Palindrome
Hvad er palindromnummer og streng?
Et palindromisk tal er en slags tal, der forbliver det samme, selv efter dets ciffer er vendt om, f.eks. 121, 1551 osv. er palindromtal, fordi de forbliver de samme, selv efter du har vendt deres ciffer om.
Samme i tilfældet med palindromstreng, siges en streng at være en palindromisk streng, når den originale streng forbliver den samme efter at have vendt strengens karakter, for eksempel er DAD en palindromisk streng.
I denne øvelse lærer du, hvordan du skriver et java-program for at kontrollere, om et heltal eller en streng er et palindrom eller ej.
Integer Palindrome i Java
1) Tjek Palindrome Integer i Java ved hjælp af While Loop
//Java Program Example for checking number is palindrome or not using while loop
//Import Scanner to take input from user
import java.util.Scanner;
//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter an integer: ");
//Taking entered number in variable of int type
int num = scn.nextInt();
//Declaring and Initilizing variables
int digit,digitSum=0,temp;
//Initilizing temp variable with the entered number
temp = num;
//Checking weather temp variable greater than 0
while(temp > 0)
{
//If Condiation is true
//Applying Modulation on temp variable
digit = temp % 10;
//Applying the formula to get the sum
digitSum = (digitSum * 10) + digit;
//Removing last digit from the number
temp = temp/10;
}
//Checking if entered number is equal to the formula sum
if(digitSum == num) {
//If Yes, Then it is a palindrom number
System.out.println(num+" IS A PALINDROME NUMBER");
}else{
//If No, Then it is not a palindrom number
System.out.println(num+" IS A NOT PALINDROME NUMBER");
}
}
}
OUTPUT:
1) Enter an integer: 121
121 IS A PALINDROME NUMBER
2) Enter an integer: 199
199 IS A NOT PALINDROME NUMBER
2) Tjek Palindrome Integer i Java ved hjælp af For Loop
//Java Program to check whether the entered number is palindrome or not using for loop
//Import Scanner to take input from user
import java.util.Scanner;
//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter an integer: ");
//Taking entered number in variable of int type
int num = scn.nextInt();
//Declaring and Initilizing variables
int digit,digitSum,temp;
//Initilizing temp variable with the entered number
temp = num;
//For loop for the iteration till temp number is greater than 0
for(digitSum = 0; temp > 0; temp /= 10){
//Applying Modulation on temp variable
digit = temp % 10;
//Applying the formula to get the sum
digitSum = (digitSum * 10) + digit;
}
//Checking if entered number is equal to the formula sum
if(digitSum == num) {
//If Yes, Then it is a palindrom number
System.out.println(num+" IS A PALINDROME NUMBER");
}else{
//If No, Then it is not a palindrom number
System.out.println(num+" IS A NOT PALINDROME NUMBER");
}
}
}
OUTPUT:
1) Enter an integer: 121
121 IS A PALINDROME NUMBER
2) Enter an integer: 123
123 IS A NOT PALINDROME NUMBER
String Palindrome i Java
3) Tjek palindromstreng i Java ved at bruge For Loop
//Java Program Example to String palindrome using for loop
//Import Scanner to take input from user
import java.util.Scanner;
//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter a string: ");
//Taking entered number in variable of int type
String str = scn.nextLine();
//Declaring and Initilizing a variable of String Type
String revStr="";
//Declaring a variable of Integer Type
int i;
//Get the length of string
int strLen = str.length();
//For loop for iteration
//Iteration will go on till the string length variable greater than equal to 0
for(i = strLen - 1; i >= 0; i--){
//Adding the reverse string and charecter at of string at `i` position
revStr = revStr + str.charAt(i);
}
//Check weather entered string equals to reverse string
if(str.equals(revStr)){
//If Yes,Then it is a palindrom string
System.out.println(str+" IS A PALINDROME STRING");
}else{
//If No,Then it is not a palindrom string
System.out.println(str+" IS A NOT PALINDROME STRING");
}
}
}
OUTPUT:
1) Enter a string: dad
dad IS A PALINDROME STRING
2) Enter a string: Java
Java IS A NOT PALINDROME STRING
4) Tjek palindromstreng i Java ved hjælp af While Loop
//Java Program Example to String palindrome using while loop
//Import Scanner to take input from user
import java.util.Scanner;
//Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter a string: ");
//Taking entered number in variable of string type
String str = scn.nextLine();
//Declaring and Initilizing a variable of String Type
String revStr="";
//Declaring a variable of Integer Type
int i;
//Get the length of string
int strLen = str.length();
//Initilizing our local integer variable with the length of string - 1
//Because we are checking minimum lenght equals to 0
i = strLen - 1;
//While Loop iteration till sting length variable greater than equals to 0
while(i >= 0){
//Adding the reverse string and charecter at of string at `i` position
revStr = revStr + str.charAt(i);
//Redusing the i value by 1
i--;
}
//Check weather entered string equals to reverse string
if(str.equals(revStr)){
//If Yes,Then it is a palindrom string
System.out.println(str+" IS A PALINDROME STRING");
}else{
//If No,Then it is not a palindrom string
System.out.println(str+" IS A NOT PALINDROME STRING");
}
}
}
OUTPUT:
1) Enter a string: dad
dad IS A PALINDROME STRING
2) Enter a string: core java
core java IS A NOT PALINDROME STRING
5) Tjek palindrom ved hjælp af Java Library-funktioner
//Java Program Example to check palindrome string using Library Function
//Import Scanner to take input from user
import java.util.Scanner;
// Main / Driver Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating an object for Scanner
Scanner scn = new Scanner(System.in);
//Printing a lable to give user an idea what to enter
System.out.print("Enter a string: ");
//Taking entered number in variable of String type
String str = scn.nextLine();
//Reversing the string with the help of Library Function
String revStr = new StringBuffer(str).reverse().toString();
//Check weather entered string equals to reverse string
if(str.equals(revStr)){
//If Yes,Then it is a palindrom string
System.out.println(str+" IS A PALINDROME STRING");
}else{
//If No,Then it is not a palindrom string
System.out.println(str+" IS A NOT PALINDROME STRING");
}
}
}
OUTPUT:
1) Enter a string: dad
dad IS A PALINDROME STRING
2) Enter a string: String
String IS A NOT PALINDROME STRING