TechoSagar: Tidy Number

Search

Google Alert - jobs

Tidy Number

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.


Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N.

Output:
For each test case in a new line print 1 if the no is a tidy number else print 0.

Constraints:
1<=T<=100
1<=N<=10^9+7

Example:
Input:

2
1234
1243
Output:
1
0
Your code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int x=-1;
int temp=1;
while(n!=0)
{
int digit=n%10;
n=n/10;
if(x>=digit)
{  temp=1;

}
else if(x<digit && x!=-1)
{

temp=0;
break;
}
x=digit;
}
if(temp==1)
{
cout<<"1"<<endl;
}
else
cout<<"0"<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook