# Check if a number is jumbled or not in C++

C++Server Side ProgrammingProgramming

Here we will see one interesting problem to check whether a number is jumbled or not. A number is said to be jumbled if, for every digit, it's neighbor digit differs by max 1. For example, a number 1223 is jumbled, but 1256 is not jumbled.

To solve this problem, we have to check if a digit has a neighbor with a difference greater than 1. If such digit is found, then return false, otherwise true.

## Example

Live Demo

#include <iostream>
#include <cmath>
using namespace std;
bool isJumbled(int number) {
if (number / 10 == 0) //for single digit number is is always jumbled
return true;
while (number != 0) {
if (number / 10 == 0) //when all digits have checked, return true
return true;
int curr_digit = number % 10;
int prev_digit = (number / 10) % 10;
if (abs(prev_digit - curr_digit) > 1)
return false;
number = number / 10;
}
return true;
}
int main() {
int n = 1223;
if(isJumbled(n)){
cout << n << " is Jumbled";
} else {
cout << n << " is not Jumbled";
}
}

## Output

1223 is Jumbled