Tidy Number (Digits in non-decreasing Order) Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report Try it on GfG Practice Given a number, the task is to check if it is tidy or not. A tidy number is a number whose digits are in non-decreasing order. Examples : Input : 1234Output : Yes Input : 1243Output : NoExplanation: Digits "4" and "3" violate the property. Asked in Freshokartz Recommended PracticeTidy NumberTry It! Algorithm: 1- One by one find all the digits.2- Compare every digit with its next digit.3- If any is in decreasing order then return false.4- Otherwise return true. Implementation : C++ // C++ program to check if a number is Tidy // or not. #include<iostream> using namespace std; // Returns true if num is Tidy bool isTidy(int num) { // To store previous digit (Assigning // initial value which is more than any // digit) int prev = 10; // Traverse all digits from right to // left and check if any digit is // smaller than previous. while (num) { int rem = num % 10; num /= 10; if (rem > prev) return false; prev = rem; } return true; } // Driver code int main() { int num = 1556; isTidy(num) ? cout << "Yes" : cout << "No"; return 0; } Java // Java program to check if a number // is Tidy or not. class Test { // Returns true if num is Tidy static boolean isTidy(int num) { // To store previous digit // (Assigning initial value // which is more than any // digit) int prev = 10; // Traverse all digits from right to // left and check if any digit is // smaller than previous. while (num!=0) { int rem = num % 10; num /= 10; if (rem > prev) return false; prev = rem; } return true; } // Driver method public static void main(String[] args) { int num = 1556; System.out.println(isTidy(num) ? "Yes" : "No"); } } Python3 # Python program to check if a number # is Tidy or not. # Returns true if num is Tidy def isTidy(num): # To store previous digit (Assigning # initial value which is more than any # digit) prev = 10 # Traverse all digits from right to # left and check if any digit is # smaller than previous. while (num): rem = num % 10 num /= 10 if rem > prev: return False prev = rem return True # Driver code num = 1556 if isTidy(num): print("Yes") else: print("No") # This code is contributed by Sharad_Bhardwaj. C# // C# program to check if a // number is Tidy or not. using System; class GFG { // Returns true if num is Tidy static bool isTidy(int num) { // To store previous digit // (Assigning initial value // which is more than any // digit) int prev = 10; // Traverse all digits from // right to left and check // if any digit is smaller // than previous. while (num != 0) { int rem = num % 10; num /= 10; if (rem > prev) return false; prev = rem; } return true; } // Driver Code public static void Main () { int num = 1556; Console.WriteLine(isTidy(num) ? "Yes" : "No"); } } // This code is contributed by m_kit PHP <?php // PHP program to check if a // number is Tidy or not. // Returns true if num is Tidy function isTidy($num) { // To store previous digit // (Assigning initial value // which is more than any // digit) $prev = 10; // Traverse all digits from // right to left and check // if any digit is smaller // than previous. while ($num) { $rem = $num % 10; $num = (int)$num / 10; if ($rem > $prev) return false; $prev = $rem; } return true; } // Driver code $num = 1556; if(isTidy($num) == true) echo "Yes"; else echo "No"; // This code is contributed by aj_36 ?> JavaScript <script> // JavaScript program for the above approach // Returns true if num is Tidy function isTidy(num) { // To store previous digit // (Assigning initial value // which is more than any // digit) let prev = 10; // Traverse all digits from right to // left and check if any digit is // smaller than previous. while (num!=0) { let rem = num % 10; num /= 10; if (rem > prev) return false; prev = rem; } return true; } // Driver Code let num = 1556; document.write(isTidy(num) ? "Yes" : "No"); // This code is contributed by susmitakundugoaldanga. </script> OutputYes Time Complexity: O(d) where d is the number of digits in the given number.Auxiliary Space: O(1) since using constant extra space. Reference : https://www.gayle.com/consulting Create Quiz Comment K kartik 1 Improve K kartik 1 Improve Article Tags : Mathematical DSA number-digits Freshokartz Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like