Bouncy Number In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Bouncy Number In C++

Bouncy Number In C++

BLUF: Mastering Bouncy Number In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Bouncy Number In C++

C++ is renowned for its efficiency. Learn how Bouncy Number In C++ enables low-level control and high-performance computing in the tutorial below.

In C++, a numeric value that does not have strictly ascending or descending digits is identified as a Bouncy Number. For instance, it displays a mixture of both patterns, such as 134468 for increasing, 987654 for decreasing, and 155349 for bouncing. Numbers with less than 100 digits do not exhibit bouncing behavior. A number is classified as bouncy when its adjacent digits are analyzed to detect alternating growth and decline. The occurrence of bouncy numbers tends to rise with larger numeric values. By developing a basic C++ script that isolates digits, evaluates patterns, and monitors the shifting trends using flags, one can effectively determine the bounciness of a number. Consequently, this process can turn into an engaging and achievable task.

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)
  • Properties of a bouncy number:

  • 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.
  • C++ implementation:

  • Bring out its numbers.
  • Check to see if it's decreasing or increasing.
  • If it's neither, label it bouncy.
  • Example 1:

Let's consider an example to demonstrate the concept of Bouncy Numbers in C++.

Example

#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:

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's consider another example to demonstrate the concept of Bouncy Number in the C++ programming language.

Example

#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:

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 summary, the numerical pattern exhibited by bouncing numbers presents a unique category of numbers that do not follow a strictly ascending or descending order. Recognizing bouncing numbers involves analyzing adjacent digits and detecting fluctuations in their values. While numbers below 100 do not exhibit bounce, as numbers increase, bouncing numbers become increasingly prevalent. Employing effective C++ strategies such as digit isolation and trend observation through basic algorithms enables straightforward identification of bouncing numbers. This concept serves as an engaging coding practice and offers a systematic approach to investigating numerical sequences and enhancing logical reasoning skills.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience