Katadrome number
Last Updated :
04 Nov, 2022
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++
#include <iostream>
using namespace std;
bool isKatadrome( int num)
{
int prev = -1;
while (num) {
int digit = num % 10;
num /= 10;
if (digit < prev)
return false ;
prev = digit;
}
return true ;
}
int main()
{
int num = 4321;
isKatadrome(num) ? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean isKatadrome( int num)
{
int prev = - 1 ;
while (num > 0 )
{
int digit = num % 10 ;
num /= 10 ;
if (digit < prev)
return false ;
prev = digit;
}
return true ;
}
public static void main(String[] args)
{
int N = 4321 ;
if (isKatadrome(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isKatadrome(num):
prev = - 1
while num:
digit = num % 10
num / / = 10
if digit < prev:
return False
prev = digit
return True
if __name__ = = '__main__' :
num = 4321
if isKatadrome(num):
print ( 'Yes' )
else :
print ( 'No' )
|
C#
using System;
class GFG{
static bool isKatadrome( int num)
{
int prev = -1;
while (num > 0)
{
int digit = num % 10;
num /= 10;
if (digit < prev)
return false ;
prev = digit;
}
return true ;
}
public static void Main()
{
int N = 4321;
if (isKatadrome(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isKatadrome( num) {
let prev = -1;
while (num > 0) {
let digit = num % 10;
num = parseInt(num/10);
if (digit < prev)
return false ;
prev = digit;
}
return true ;
}
let N = 4321;
if (isKatadrome(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(d) where d is number of digits in given number.
Reference: http://www.numbersaplenty.com/set/katadrome/
Share your thoughts in the comments
Please Login to comment...