In C++, a number whose digits are neither strictly growing nor strictly falling is called a Bouncy Number. For example, it exhibits both trends, with 134468 increasing, 987654 falling, and 155349 bouncing. Numbers with fewer than 100 digits don't bounce. A number can be considered bouncy if its neighbouring digits are compared to see if some grow while others decrease. The frequency of the bouncy numerals increases with the numbers. A basic C++ program program that extracts digits, examines trends, and tracks growing and decreasing patterns with flags can be used to calculate buoyancy. As a result, the activity becomes enjoyable and doable.
Example:
- Increasing numbers: 134468 (digits increase or remain constant)
- Decreasing numbers: 987654 (digits decrease or remain constant)
- Bouncy numbers: 155349 (digits are neither in increasing nor decreasing order)
- Any number less than 100 is not bouncy because there are no three digits to compare that can both rise and fall.
- The first bouncy number is 101.
- Their frequency rises in tandem with the numbers.
- Bring out its numbers.
- Check to see if it's decreasing or increasing.
- If it's neither, label it bouncy.
Properties of a bouncy number:
C++ implementation:
Example 1:
Let us take an example to illustrate the Bouncy Number in C++ .
#include <iostream>
using namespace std;
// Function to check if a number is bouncy
bool isBouncy(int num) {
bool isIncreasing = false;
bool isDecreasing = false;
int previousDigit = num % 10; // Start with the last digit
num /= 10;
while (num > 0) {
int currentDigit = num % 10;
if (currentDigit < previousDigit)
isIncreasing = true;
else if (currentDigit > previousDigit)
isDecreasing = true;
// If both increasing and decreasing trends are found
if (isIncreasing && isDecreasing)
return true;
previousDigit = currentDigit;
num /= 10;
}
return false; // Number is not bouncy
}
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (isBouncy(number))
cout << number << " is a Bouncy Number." << endl;
else
cout << number << " is NOT a Bouncy Number." << endl;
return 0;
}
Output:
Enter a number: 101
101 is a Bouncy Number.
Enter a number: 567
567 is NOT a Bouncy Number.
Explanation:
- A number is entered by the user.
- Digit-to-Digital Comparison: The software verifies every pair of numbers: When a pair grows, the isIncreasing flag is set.
- It sets the isDecreasing flag if any pair decreases.
- The result is that the number is categorized as bouncy if both flags are true, and not bouncy otherwise.
Example 2:
Let us take another example to illustrate the Bouncy Number in C++.
#include <iostream>
using namespace std;
// Function to determine if a number is bouncy
bool isBouncy(int num) {
if (num < 100)
return false; // Numbers less than 100 are not bouncy
bool increasing = false;
bool decreasing = false;
int lastDigit = num % 10; // Start with the last digit
num /= 10;
while (num > 0) {
int currentDigit = num % 10;
if (currentDigit < lastDigit)
increasing = true;
else if (currentDigit > lastDigit)
decreasing = true;
// If both increasing and decreasing trends are found
if (increasing && decreasing)
return true;
lastDigit = currentDigit; // Update for the next iteration
num /= 10;
}
return false; // Not a bouncy number
}
int main() {
// Ask the user to input a number
int num;
cout << "Enter a number to check if it is bouncy: ";
cin >> num;
// Check and display result
if (isBouncy(num))
cout << num << " is a Bouncy Number." << endl;
else
cout << num << " is NOT a Bouncy Number." << endl;
return 0;
}
Output:
Enter a number to check if it is bouncy: 356
356 is NOT a Bouncy Number.
Enter a number to check if it is bouncy: 106
106 is a Bouncy Number.
Enter a number to check1 if it is bouncy: 155349
155349 is a Bouncy Number.
Explanation:
- Edge Case: Non-bouncy numbers are those that are less than 100.
- Digit-to-Digital Comparison: If the sequence gets longer, set increasing = true for every pair of digits. Setting declining = true will occur if it falls.
- Check for Bouncy: The function returns true if both trends are seen. Whether the input number is bouncy is shown by the output.
Conclusion:
In conclusion, the digit sequence of bouncing numbers is an interesting class of numbers that is neither completely increasing nor decreasing. The key to understanding and identifying bouncing numbers is comparing neighbouring digits and searching for patterns of growth and decline. Despite the fact that numbers below 100 are never bouncy, bouncy numbers become more common as numbers get larger. By using efficient C++ techniques like digit extraction and trend tracking with simple logic, it is simple to determine whether a number is bouncing. This concept is not only an enjoyable programming exercise, but it is also a practical way to explore number patterns and logical thinking.