Write a program to check if the given number is palindrome or not.
Palindrome Number: A number that reads the same from start and end is a palindrome number.
Example:
Input: num = 121
Output: Number is palindrome
Approach: Find the reverse of the number, if the reverse is the same as the original number then the number is a palindrome, otherwise the number is not a palindrome.
C
#include <stdio.h>int main(){int num = 121;int n = num;int rev = 0;while (n > 0){int temp = n % 10;rev = rev * 10 + temp;n = n / 10;}if (num == rev){printf("Number is palindrome ");}else{printf("Number is not palindrome ");}return 0;}
Java
class CheckNumberPalindrome {public static void main(String args[]) {int number=121;if(isPalindrome(number))System.out.println("Number "+number+" is Palindrome");elseSystem.out.println("Number "+number+" is not Palindrome");}public static boolean isPalindrome(int x) {int or=x;if(x<0) return false;long rev=0;while(x!=0){int m=x%10;rev=rev*10+m;x=x/10;}int r=(int)rev;if(r==or)return true;elsereturn false;}}
C++
#include <bits/stdc++.h>using namespace std;//Function to check for given//number is palindrome or notbool isPalindrome(int n){int rev=0,temp=n;while(temp!=0){int last=temp%10;rev=rev*10+last;temp=temp/10;}if(n==rev)return true;elsereturn false;}int main(){int n=121;if(isPalindrome(n))cout<<"Number is palindrome\n";elsecout<<"Number is not palindrome\n";}
No comments:
Post a Comment