
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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 number is Quartan Prime or not in C++
- Check if a given number is sparse 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++
- Swift Program to Check If a Number is Spy number 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++
- Check if a number is divisible by 41 or not in C++
- Check if a number is power of 8 or not in C++

Advertisements