3 approaches to check palindrome number in java codeyourslf

In this article you can learn how to check whether a number is palindrome or not in java programming language with 3 different approaches. Codeurself

palindrome number in java | codeyourslf

3 approaches to check palindrome number in java codeyourslf

In this article you can learn how to check whether a number is palindrome or not in java programming language with 3 different approaches.

Approach:-1 "palindrome number in java using for loop"

 import java.util.Scanner;

public class usingForLoop {

    public static void main(String[] args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the number: ");

      int num = sc.nextInt();

      int temp = num;

      int rev = 0;

      for(int i = 0; num > 0; i++){

        int lastDigit = num % 10;

        rev = rev * 10 + lastDigit;

        num = num / 10;

      }

      //System.out.println(rev);

      if(temp == rev){

        System.out.println("Nunber is palindrome");

      }

      else{

        System.out.println("Number is not palindrome");

      }

    }

}
+
Explanation

If a number is equal to it's reverse number then we can say the number is palindrome number. Ex :- 121, it's reverse form also 121, So it is a palindrome number. 

 👉 Here using for loop we can reverse the number and in if else statement we have to check whether the original number is same with the reverse one or not. 

If both are same, we just have to print this number is palindrome. I have explained the reverse of a number, follow this to easily understood the concept. Click here

Output

3 approaches to check palindrome number in java codeyourslf

Approach:-2 "palindrome number or not in java using while loop"

  public class usingWhileLoop {

    public static void main(String[] args){

      int num = 121;

      int temp = num;

      int rev = 0;

      while(num != 0){

        rev = rev * 10 + num % 10;

        num = num / 10;

      }

      if(rev == temp){

        System.out.println("Number is palindrome");

      }

      else{

        System.out.println("Number is not palindrome");

      }

    }

}

 
+
Explanation

- In while loop the logic remain same, for better understanding I have used both for and while loop.

Approach:-3 "palindrome or not in java using ternary operator"

  public class usingTernaryOperator {

    public static void main(String args[]){

      int num = 181;

      int temp = num;

      int rev = 0;

      while(num != 0){

        rev = rev * 10 + num % 10;

        num = num / 10;

      }

      System.out.println((rev == temp)?"Palindrome Number":"Not Palindrome Number");

    }

} 
+
Explanation

Ternary operator is like if else condition, we can use ternary operator in place of if else statement to find palindrome number in java. 

We have to write the logic and after that we can check for original number and reverse number is same or not in ternary operator.


- If you learn something new from this article please share with your friends.
- If you have any questions please feel free to write it on the comment below 👇
`

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin