Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Locating a 5 within a number

I’ve spent nearly a day attempting to create a program that can locate the digit 5 in a positive integer. If it does find a 5 in the remainder of the number, the program will display that a five was found; otherwise, it will display that no five was found. However, if there is a negative integer, the program will prompt the user to try again until a positive integer is entered. The source code for this program is provided below.

#include <iostream> // using the input and output library
  
using namespace std; // using standard in order to not include the scope resolution operator (std::)

int main() {
        int num {0}; // The number
        int remainder; // The remainder for the number


        cout << "Enter a positive integer: "; // Asking the user to place a positive integer
        cin >> num; // The input
        while (num < 0) {cout << "Bad input! Try again: "; cin >> num;} // As long as the number is less than zero, it is a bad input and the user must try again until the input is a positive integer.


       while (num > 0)
       { remainder = num % 10;
          if (remainder != 5)
        num /= 10;
        } // As long as number is greater than 0, the remainder is number modulus 10 and if the remainder is not equal to 5, the number will be assigned when divided by 10.

        if (remainder != 5) {cout << "Not five";} // If the remainder isn't five, there is no five.
        else {cout << "Five";} // Otherwise, there is a five.
        cout << endl; // Ends the line.

        return 0; // The program is terminated.
}

The difficulty I’ve been having is while the outputs of the negative integer and no five detected are correct, the detection of five does not display the intended output. I’ve tried changing the contents of the second while statement and even placing a cout for the result, but the loop continued to run. I’ve been stuck for almost a day attempting to find a solution without success; if anyone can provide assistance, I would be extremely grateful.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In this loop, whenever remainder == 5 you will just continue the loop, over and over again and never end.

while (num > 0) {
    remainder = num % 10;
    if (remainder != 5)
        num /= 10;
    else
        continue;
}

You need to replace continue with break to exit the loop when a 5 is found:

while (num > 0) {
    remainder = num % 10;
    if (remainder == 5) break;
    num /= 10;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading