

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a number is jumbled or not in C++
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
#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
- Related Questions & Answers
- Check if a number is a Krishnamurthy Number or not in C++
- Check if a number is an Unusual Number or not in C++
- Check if a number is an Achilles number or not in Python
- Check if a number is an Achilles number or not in C++
- Check if a given number is sparse or not in C++
- Check if a number is Quartan Prime or not in C++
- Check if a number is Primorial Prime or not in Python
- Check if a number is Primorial Prime or not in C++
- Check if given number is Emirp Number or not in Python
- Check if a number is a Pythagorean Prime or not in C++
- C# Program to check if a number is prime or not
- Python program to check if a number is Prime or not
- PHP program to check if a number is prime or not
- Check if a number is in given base or not in C++
- Check if a number is divisible by 23 or not in C++
Advertisements