Open In App

Reversible numbers

A number is said to be a reversible if sum of the number and its reverse had only odd digits. The problem is to find out if a number is reversible or not. 

Examples: 



Input: 36 
Output: Reversible number
as 36 + 63 = 99 has only odd digits.

Input: 409 
Output: Reversible number
as 409 + 904 = 1313 has only odd digits.

Input: 35 
Output: Not Reversible number
as 35 + 53 = 88 has only odd digits

Naive Method

Calculate reverse of each number and add it to the number. If the resultant is reversible increment the value of count. Calculate this for every number from 1 to n. 



Time complexity: O(10^n) as it should calculate reverse of each number.

Advance method 

Generalizing the Solution:  

Program To check if a number is reversible or not 




// C++ program to check if a given
// number is reversible or not
#include <iostream>
#include <cmath>
using namespace std;
  
// Function to check reversible number
void checkReversible (int n)
{
    int rev = 0, rem;
  
    // Calculate reverse of n
    int flag = n;
    while (flag)
    {
        rem = flag % 10;
        rev *= 10;
        rev += rem;
        flag /= 10;
    }
  
    // Calculate sum of number
    // and its reverse
    int sum = rev + n;
  
    // Check for reverse number
    // reach digit must be odd
    while (sum && (rem % 2 != 0))
    {
        rem = sum % 10;
        sum /= 10;
    }
  
    if (sum == 0)
        cout << "Reversible Number";
    else
        cout << "Non-Reversible Number";
}
  
// Driver function
int main()
{
    int n = 36;
    checkReversible(n);
    return 0;
}




// Java program to check if a given
// number is reversible or not
import java.io.*;
  
class GFG {
      
    // Function to check reversible number
    static void checkReversible (int n)
    {
        int rev = 0, rem = 0;
      
        // Calculate reverse of n
        int flag = n;
        while (flag>0)
        {
            rem = flag % 10;
            rev *= 10;
            rev += rem;
            flag /= 10;
        }
      
        // Calculate sum of number
        // and its reverse
        int sum = rev + n;
      
        // Check for reverse number
        // reach digit must be odd
        while (sum > 0 && (rem % 2 != 0))
        {
            rem = sum % 10;
            sum /= 10;
        }
      
        if (sum == 0)
            System.out.println("Reversible Number");
        else
            System.out.println("Non-Reversible Number");
    }
      
    // Driver function
    public static void main (String[] args)
    {
        int n = 36;
        checkReversible(n);
          
    }
}
// This code is contributed by vt_m.




# Python3 program to check if a given 
# number is reversible or not 
  
# Function to check reversible number
def checkReversible (n):
      
    rev = 0
    
    # Calculate reverse of n
    flag = n
      
    while (flag != 0):
        rem = flag % 10
        rev *= 10
        rev += rem
        flag //= 10
      
    # Calculate sum of number
    # and its reverse
    sum = rev + n
    
    # Check for reverse number
    # reach digit must be odd
    while (sum and ((rem % 2) != 0)):
        rem = sum % 10
        sum //= 10
      
    if (sum == 0):
        print("Reversible Number")
    else:
        print("Non-Reversible Number")
  
# Driver Code
n = 36
  
checkReversible(n)
  
# This code is contributed by sanjoy_62




// C# program to check if a given
// number is reversible or not
using System;
  
class GFG {
      
    // Function to check reversible number
    static void checkReversible (int n)
    {
        int rev = 0, rem = 0;
      
        // Calculate reverse of n
        int flag = n;
        while (flag > 0)
        {
            rem = flag % 10;
            rev *= 10;
            rev += rem;
            flag /= 10;
        }
      
        // Calculate sum of number
        // and its reverse
        int sum = rev + n;
      
        // Check for reverse number
        // reach digit must be odd
        while (sum > 0 && (rem % 2 != 0))
        {
            rem = sum % 10;
            sum /= 10;
        }
      
        if (sum == 0)
        Console.WriteLine("Reversible Number");
        else
        Console.WriteLine("Non-Reversible Number");
    }
      
    // Driver function
    public static void Main () 
    {
        int n = 36;
          checkReversible(n);
          
    }
}
  
// This code is contributed by vt_m.




// JavaScript program to check if a given 
// number is reversible or not 
  
// Function to check reversible number
function checkReversible (n)
{    
    var rev = 0;
    
    // Calculate reverse of n
    var flag = n;
      
    while (flag != 0)
    {
        rem = flag % 10
        rev *= 10
        rev += rem
        flag = Math.floor(flag / 10);
    
      
    // Calculate sum of number
    // and its reverse
    var sum = rev + n;
    
    // Check for reverse number
    // reach digit must be odd
    while (sum && ((rem % 2) != 0))
    {
        rem = sum % 10
        sum = Math.floor(sum / 10);
    }
      
    if (sum == 0)
        console.log("Reversible Number")
    else
        console.log("Non-Reversible Number")
}
  
// Driver Code
let n = 36
checkReversible(n);
  
// This code is contributed by phasing17

Output: 

Reversible Number

Time complexity: O(log N) where N is no of digits of number n

 Program To count total reversible numbers upto n




// C++ program to find the count of
// reversible numbers upto a given number n
#include <iostream>
#include <cmath>
using namespace std;
  
// Function to calculate the count of reversible number
void countReversible (int n)
{
    int count = 0;
  
    // Calculate counts of
    // reversible number of 1 to n-digits
    for ( int i = 1; i <= n; i++)
    {
        // All four possible cases and their formula
        switch (i % 4)
        {
  
        // for i of form 2k
        case 0:
        case 2:
            count += 20 * pow( 30, ( i / 2 - 1));
            break;
  
        // for i of form 4k + 3
        case 3:
            count += 100 * pow ( 500, i / 4 );
            break;
  
        // for i of form 4k + 1 no solution
        case 1:
            break;
        }
    }
    cout << count;
}
  
// Driver function
int main()
{
    // count up-to 9 digit numbers (1 billion)
    int n = 9;
    countReversible(n);
    return 0;
}




// Java program to find the count of
// reversible numbers upto a given number n
import java.io.*;
  
class GFG {
  
    // Function to calculate the count 
    // of reversible number
    static void countReversible (int n)
    {
        int count = 0;
      
        // Calculate counts of
        // reversible number of 1 to n-digits
        for ( int i = 1; i <= n; i++)
        {
            // All four possible cases 
            // and their formula
            switch (i % 4)
            {
      
            // for i of form 2k
            case 0:
            case 2:
                count += 20 * Math.pow( 30, ( i / 2 - 1));
                break;
      
            // for i of form 4k + 3
            case 3:
                count += 100 * Math.pow ( 500, i / 4 );
                break;
      
            // for i of form 4k + 1 no solution
            case 1:
                break;
            }
        }
        System.out.println(count);
    }
      
    // Driver function
    public static void main (String[] args)
    {
        // count up-to 9 digit numbers (1 billion)
        int n = 9;
        countReversible(n);
          
    }
}
// This code is contributed by vt_m.




# Python3 program to find the count of
# reversible numbers upto a given number n
  
# Function to calculate the count of reversible number
def countReversible (n):
    count = 0;
   
    # Calculate counts of
    # reversible number of 1 to n-digits
    for i in range(1, n + 1):
      
        # All four possible cases and their formula
        case =  i % 4
          
   
        # for i of form 2k
        if case == 0 or case == 2:
            count += 20 * pow( 30, ( i // 2 - 1));
  
        # for i of form 4k + 3
        elif case == 3:
            count += 100 * pow ( 500, (i // 4) );
  
        # for i of form 4k + 1 no solution
        elif case == 1:
            pass;
          
    print(count);
   
# Driver function
  
# count up-to 9 digit numbers (1 billion)
n = 9;
countReversible(n);
  
# This code is contributed by phasing17




// C# program to find the count of
// reversible numbers upto a given number n
using System;
  
class GFG {
      
    // Function to calculate the 
    // count of reversible number
    static void countReversible (int n)
    {
        int count = 0;
      
        // Calculate counts of
        // reversible number of 1 to n-digits
        for ( int i = 1; i <= n; i++)
        {
            // All four possible cases 
            // and their formula
            switch (i % 4)
            {
      
            // for i of form 2k
            case 0:
            case 2:
                count += 20 * (int)Math.Pow( 30, ( i / 2 - 1));
                break;
      
            // for i of form 4k + 3
            case 3:
                count += 100 * (int)Math.Pow ( 500, i / 4 );
                break;
      
            // for i of form 4k + 1 no solution
            case 1:
                break;
            }
        }
        Console.WriteLine(count);
    }
      
    // Driver function
    public static void Main () 
    {
        // count up-to 9 digit numbers (1 billion)
        int n = 9;
        countReversible(n);
          
    }
}
  
// This code is contributed by vt_m.




// JavaScript program to find the count of
// reversible numbers upto a given number n
  
  
// Function to calculate the count of reversible number
function countReversible (n)
{
    let count = 0;
   
    // Calculate counts of
    // reversible number of 1 to n-digits
    for (let i = 1; i <= n; i++)
    {
        // All four possible cases and their formula
        switch (i % 4)
        {
   
        // for i of form 2k
        case 0:
        case 2:
            count += 20 * Math.pow( 30, Math.floor( i / 2 - 1));
            break;
   
        // for i of form 4k + 3
        case 3:
            count += 100 * Math.pow ( 500, Math.floor(i / 4) );
            break;
   
        // for i of form 4k + 1 no solution
        case 1:
            break;
        }
    }
    console.log (count);
}
   
   
// Driver function
  
// count up-to 9 digit numbers (1 billion)
let n = 9;
countReversible(n);
  
  
  
// This code is contributed by phasing17

Output: 

608720

Time complexity : O(nlogn) 
Auxiliary Space : O(1)

Reference : Project Euler 145: How many reversible numbers are there below one-billion? 

 


Article Tags :