Tidy Number (Digits in non-decreasing Order)
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 : 1234 Output : Yes Input : 1243 Output : No Digits "4" and "3" violate the property.
Asked in Freshokartz
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; } |
chevron_right
filter_none
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"); } } |
chevron_right
filter_none
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 = 1556if isTidy(num): print("Yes") else: print("No") # This code is contributed by Sharad_Bhardwaj. |
chevron_right
filter_none
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 |
chevron_right
filter_none
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"; elseecho "No"; // This code is contributed by aj_36 ?> |
chevron_right
filter_none
Output :
Yes
Time Complexity : O(d) where d is number of digits in given number.
Reference :
https://www.careercup.com/question?id=5136136486780928
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Check if a number has digits in the given Order
- Find the closest and smaller tidy number
- Largest number smaller than or equal to n and digits in non-decreasing order
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Generate k digit numbers with digits in strictly increasing order
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check if the sum of digits of number is divisible by all of its digits
- Smallest number with given sum of digits and sum of square of digits
- Sum of the digits of square of the given number which has only 1's as its digits
Improved By : jit_t


