Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Create a C++ file. Prompt the user for a positive integer, validating the input until the user enters a positive integer. If the number contains the digit 5 at any position, then print The number contains the digit 5. else print The number does not contain the digit 5.

Expert Solution
Check Mark
Step 1: Algorithm for Checking if a Number Contains the Digit 5
  1. Start.
  2. Initialize an integer variable num to store the user input.
  3. Display the message "Enter a positive integer."
  4. Repeat the following steps until a valid positive integer is entered:
    a. Read the user input into the variable num.
    b. If the input is not an integer or is less than or equal to 0, clear the input stream, ignore any remaining characters, and display the message "Invalid input. Please enter a positive integer."
    c. Otherwise, exit the loop.
  5. Define a function containsDigit5 that takes an integer number as a parameter.
    a. Initialize a while loop with the condition that number is greater than 0.
    b. Inside the loop, check if the last digit of number (i.e., number % 10) is equal to 5.
    c. If the last digit is 5, return true to indicate that the number contains the digit 5.
    d. If not, divide number by 10 to remove the last digit and continue the loop.
    e. If the loop completes without finding a 5, return false to indicate that the number does not contain the digit 5.
  6. Call the containsDigit5 function with the num as the argument.
  7. If the function returns true, display the message "The number contains the digit 5."
  8. If the function returns false, display the message "The number does not contain the digit 5."
  9. End.
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education