Solving the mystery of 12.02.2021 !!
Tags: beginners, programming, tutorial
Published: 2021-02-12 18:05:26

We come across symmetry on a daily basis in our lives in many different ways, in both the natural and man-made world. Symmetry possesses the magic power of drawing our attention. Most of us value and enjoy experiencing symmetry both visually and mentally because it conveys a sense of aesthetic balance, beauty, equality, and evenness.

Today's date is **"12-02-2021"** which is unique in its own right because it is one of the 12 palindromic dates of the 21st century. If we see the date in **DD-MM-YYYY** format it is the same sequence of numbers if we see it in forward or backward way. Thus we can see that palindromes are also possessing symmetry.

> A palindrome is a symmetric word, phrase, number, or another sequence of units that reads the same forward and backward.

Some interesting examples of sentences that are palindromic are - *"Never odd or even"*, *"No, it is opposition"*, *"No lemons, no melon"*, *"Rats live on no evil star"*, and *"Rise to vote, Sir"*.

In computer programming, the program to find whether a string or number is palindrome or not is very common. It is one of the first problems taught to new learners as it explains the concept of conditional statements like if-else and for and while loops to the beginners.

In this post, I will try to write programs in some famous programming languages to solve the problem of checking whether a number is a palindrome or not.


## C
In C language the code for finding whether a number is a palindrome or not is given below.
```C
#include <stdio.h>

int main(void) {
    int n, reverse = 0, rem, original;
    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    // reversing a number and saving it in variable reverse
    // find the remainder with 10 and add it to reverse and each step multiply by 10
    // divide the number by 10 until it is 0
    while (n != 0) {
        rem = n % 10;
        reverse = reverse * 10 + rem;
        n /= 10;
    }

    // checking condition for palindrome i.e. if orignal and reverse are equal
    if (original == reverse)
        printf("%d is a palindrome.", original);
    else
        printf("%d is not a palindrome.", original);

    return 0;
}
```
<hr>

## C++
In C++ the code is similar to C with difference only in taking the input and printing output.

```c++
#include <iostream>
using namespace std;

int main() {
    int n, reverse = 0, rem, original;
    cout<<"Enter a number: ";
    cin>>n;
    original = n;

    // reversing a number and saving it in variable reverse
    // find the remainder with 10 and add it to reverse and each step multiply by 10
    // divide the number by 10 until it is 0
    while (n != 0) {
        rem = n % 10;
        reverse = reverse * 10 + rem;
        n /= 10;
    }

    // checking condition for palindrome i.e. if orignal and reverse are equal
    if (original == reverse)
        cout<<original<<" is a palindrome."<<endl;
    else
       cout<<original<<" is not a palindrome."<<endl;

    return 0;
}
```
<hr>

## Java
The code in Java is given below the logic is the same as mentioned above in the case of C and C++.

```java
import java.io.*;
import java.util.Scanner; 

class Palindrome{  
 public static void main(String args[]){  
  int rem,reverse=0,original;    
  int n;
  
  System.out.println("Enter the number: ");
  
  Scanner s = new Scanner(System.in); 
  n = s.nextInt();
  
  original=n;    
  while(n>0){    
   rem=n%10;    
   reverse=(reverse*10)+rem;    
   n=n/10;    
  }    
  if(reverse==original)    
   System.out.println(original+ " is a palindrome number ");    
  else    
   System.out.println(original+ " is not a palindrome number");    
}  
}  
```
<hr>

## Python
The code in Python is simpler as there are less overheads for import and input-output operations. 

```python
print("Enter a number")
n= int(input())

original = n
rem,reverse=0,0


while(n!=0):
    rem=n%10    
    reverse=(reverse*10)+rem    
    n=n//10

if reverse == original:
    print(original ,"is a palindrome number")
else:
    print(original ,"is not a palindrome number")
    
```
If we treat the input as a string and use a string splicing method of python the code becomes much more smaller.

```python
print("Enter a number")
n= input()

reverse= n[::-1]

if reverse == n:
    print(n ,"is a palindrome number")
else:
    print(n ,"is not a palindrome number")
```
<hr>  
  
## Perl
A similar program in Perl would look like this

```perl
print"Enter a number: ";
$n=<STDIN>;

$original=$n;
$reverse=0;

while($n>0)
{
$rem=$n%10;
$reverse=($reverse*10)+$rem;
$n=int($n/10);
}


if($original==$reverse)
{
print "$original is palindrome\n";
}
else
{
print "$original is not palindrome\n";
}
```   
<hr>

## Ruby

The program in Ruby to check whether a given number is palindrome or not is given below.

```ruby
puts "Enter the number"
num=gets.chomp.to_i

original=num
reverse = 0

while num!=0 
    rem=num%10
    num=num/10
    reverse=reverse*10+rem
end

if(original==reverse)
    puts "#{original} is a palindrome"
else
    puts "#{original} is not a palindrome"
end
```
A code in which the number is converted to string and then reversed and then converted back to integer and this reversed number is compared with original is given below

```ruby
puts "Enter the number"
num=gets.chomp.to_i

if(num.to_s.reverse.to_i==num)
    puts "#{num} is a palindrome"
else
    puts "#{num} is not a palindrome"
end
```
<hr>

Hope the beginners got an idea about how to use conditional statements and loops in various languages and to check palindrome numbers using this article and it was beneficial for them.