Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Palindrome number

Easy
0/40
201 upvotes

Problem statement

Check whether a given number ’n’ is a palindrome number.

Note :
Palindrome numbers are the numbers that don't change when reversed.
You don’t need to print anything. Just implement the given function.
Example:
Input: 'n' = 51415
Output: true
Explanation: On reversing, 51415 gives 51415.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line of the input contains the number 'n'.
Output format:
Return a boolean value True or False.
Sample Input 1 :
1032
Sample Output 1 :
false
Explanation Of Sample Input 1:
1032, on being reversed, gives 2301, which is a totally different number.
Sample Input 2 :
121
Sample Output 2 :
true
Explanation Of Sample Input 2:
121, on being reversed, gives 121, which is the same.
Expected time complexity:
The expected time complexity is O(log(n)).
Constraints :
1 <= n <= 10^9
Time Limit: 1 sec
Palindrome number
All tags
Sort by
Search icon

Interview problems

2 liner C++ Solution in O(n) time complexity

Note:- Since the question didn't mentioned that to solve it without type-casting the value of n from integer to string, I tried this method.

 

  • Time Complexity:- O(n)
  • Space Complexity:- 0(n)

 

 

bool palindrome(int n) {

    // Write your code here 

 while(n>=0){  

    string str= to_string(n);            if(str==string(str.rbegin(),  str.rend())) return true;    

 else return false;  

   }

 }

24 views
0 replies
1 upvote

Interview problems

PalindromeNumber Java

public class Solution {

    public static boolean palindromeNumber(int n){

        // Write your code here.

        int duplicate =n;

        int reverseNumber=0;

 

        while(n>0){

            int rem=n%10;

            reverseNumber=(reverseNumber*10)+rem;

            n=n/10;

        }

 

        if(duplicate==reverseNumber){

            return true;

        }

        else{

            return false;

        }

    }

}

26 views
0 replies
0 upvotes

Interview problems

solution

bool palindrome(int n)

{

    // Write your code here

    

    int sum=0;

    int original=n;

    int last=0;

    while(n>0){

        last=n%10;

        n=n/10;

        sum=sum*10+last;

    }

    if(sum==original){

        return true;

    }

    else{

        return false;

    }

 

    

}

26 views
0 replies
1 upvote

Interview problems

Simple java code

public class Solution {
    public static boolean palindromeNumber(int n){
        // Write your code here.
        int dup =n;
        int revnum=0;
        while(n>0){
            int rem = n%10;
            revnum = (revnum*10)+rem;
            n=n/10;
        }
        if(dup==revnum){
            return true;
        }else{
            return false;
        }
    }
}
26 views
0 replies
0 upvotes

Interview problems

Palindrome cpp easy

bool palindrome(int n)

{

int ori = n;

int rev = 0;

while(n>0) //use while loop as the iteration depends on n not on i 

{

int dig = n%10;

rev = (rev * 10) + dig;

n = n/10;

}

if(ori == rev)

{

return true;

}

return false;

 

}

47 views
0 replies
0 upvotes

Interview problems

test case not passing?

n = int(input())

 

def palindrome(n):

reverse = 0

org = n

 

while n > 0:

digit = n % 10

reverse = reverse * 10 + digit

n //= 10

 

return org == reverse

 

if palindrome(n):

print(f"{n} is a palindrome")

else:

print(f"{n} is not a palindrome")

why this is not working?

52 views
0 replies
1 upvote

Interview problems

java best solution

public class Solution {

    public static boolean palindromeNumber(int n){

        // Write your code here.

        int rev=0;

        int orginal=n;

         boolean flag=false;

        while(n!=0){

          int last=n%10;

         rev=(rev*10)+last;

            n=n/10;

         

        }if(rev==orginal) flag=true;

        return flag;

       

    }

}

41 views
0 replies
1 upvote

Interview problems

java code

import java.util.*;

 

public class Solution { 

 

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        int n = sc.nextInt();

        int originalNum = n; 

        int RevNum = 0;

        

        

        while (n > 0) {

            int LastDigit = n % 10; 

            RevNum = (RevNum * 10) + LastDigit; 

            n = n / 10; 

        }

 

        

        System.out.println(originalNum == RevNum ? "true" : "false");

 

        sc.close(); 

    }

}

 

java

25 views
0 replies
0 upvotes

Interview problems

palindrome number for beginner it take more time but it is understandable

bool palindrome(int n)

{

    int original_n=n;

    int reverse_no=0;

    while (n > 0) {

      int last_digit = n % 10;

      reverse_no = reverse_no* 10 + last_digit;

      n = n / 10;

    }

     if(reverse_no==original_n){

        return true;

    }else{

    return false;

    

}

}

91 views
0 replies
0 upvotes

Interview problems

Palindrome number

bool palindrome(int n)

{

    int reverse = 0;

    int orignal = n;

 

    while(n>0){

        int lastdigit = n%10;

        reverse = reverse*10+lastdigit;

        n = n/10;

    }

    if(orignal==reverse){

        return true;

    }

    else{

        return false;

    }

 

56 views
0 replies
0 upvotes
Full screen
Console