Open In App

Katadrome number

Last Updated : 04 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A Katadrome number is a number whose digits are in decreasing order.
Few Katadrome numbers are: 

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21…. 

Check if N is a Katadrome

Given a number N, the task is to check if it is Katadrome or not.
Examples: 
 

Input: 4321 
Output: Yes
Input: 1243 
Output: No 

Approach: The idea is to traverse the digits of the number and check if the current digit is less than the last digit. If all the digits satisfy the conditions then the number is a Katadrome number. 
Below is the implementation of the above approach:

C++




// C++ implementation to check if
// a number is Katadrome or not.
 
#include <iostream>
using namespace std;
 
// Function to check if a number
// is a Katadrome number or not
bool isKatadrome(int num)
{
    // To store previous digit (Assigning
    // initial value which is less than any
    // digit)
    int prev = -1;
 
    // Traverse all digits from right to
    // left and check if any digit is
    // smaller than previous.
    while (num) {
        int digit = num % 10;
        num /= 10;
        if (digit < prev)
            return false;
        prev = digit;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int num = 4321;
    isKatadrome(num) ? cout << "Yes"
                     : cout << "No";
    return 0;
}


Java




// Java implementation to check if
// a number is Katadrome or not.
class GFG{
 
// Function to check if a number
// is a Katadrome number or not
static boolean isKatadrome(int num)
{
     
    // To store previous digit
    // (Assigning initial value
    // which is less than any digit)
    int prev = -1;
 
    // Traverse all digits from right
    // to left and check if any digit
    // is smaller than previous.
    while (num > 0)
    {
        int digit = num % 10;
        num /= 10;
        if (digit < prev)
            return false;
        prev = digit;
    }
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4321;
 
    // Function Call
    if (isKatadrome(N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Pratima Pandey


Python3




# Python3 program to print count of values such
# that n+i = n^i
 
def isKatadrome(num):
     
    # To store previous digit (Assigning
    # initial value which is less than any
    # digit)
    prev = -1
     
    # Traverse all digits from right to
    # left and check if any digit is
    # smaller than previous.
    while num:
        digit = num % 10
        num //= 10
        if digit < prev:
            return False
        prev = digit
         
    return True
 
# Driver code
if __name__=='__main__':
     
    num = 4321
     
    if isKatadrome(num):
        print('Yes')
    else:
        print('No')
 
# This code is contributed by rutvik


C#




// C# implementation to check if
// a number is Katadrome or not.
using System;
class GFG{
 
// Function to check if a number
// is a Katadrome number or not
static bool isKatadrome(int num)
{
     
    // To store previous digit
    // (Assigning initial value
    // which is less than any digit)
    int prev = -1;
 
    // Traverse all digits from right
    // to left and check if any digit
    // is smaller than previous.
    while (num > 0)
    {
        int digit = num % 10;
        num /= 10;
        if (digit < prev)
            return false;
        prev = digit;
    }
    return true;
}
 
// Driver Code
public static void Main()
{
    int N = 4321;
 
    // Function Call
    if (isKatadrome(N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
 
// Javascript implementation to check if
// a number is Katadrome or not.
 
 
    // Function to check if a number
    // is a Katadrome number or not
    function isKatadrome( num) {
 
        // To store previous digit
        // (Assigning initial value
        // which is less than any digit)
        let prev = -1;
 
        // Traverse all digits from right
        // to left and check if any digit
        // is smaller than previous.
        while (num > 0) {
            let digit = num % 10;
            num = parseInt(num/10);
            if (digit < prev)
                return false;
            prev = digit;
        }
        return true;
    }
 
    // Driver Code
      
        let N = 4321;
 
        // Function Call
        if (isKatadrome(N))
            document.write("Yes");
        else
            document.write("No");
 
// This code contributed by Rajput-Ji
 
</script>


Output: 

Yes

 

Time Complexity: O(d) where d is number of digits in given number.
Reference: http://www.numbersaplenty.com/set/katadrome/
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads