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.
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.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
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
Step 1: Algorithm for Checking if a Number Contains the Digit 5
- Start.
- Initialize an integer variable num to store the user input.
- Display the message "Enter a positive integer."
- 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. - 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. - Call the containsDigit5 function with the num as the argument.
- If the function returns true, display the message "The number contains the digit 5."
- If the function returns false, display the message "The number does not contain the digit 5."
- End.
Step by step
Solved in 3 steps with 3 images
Knowledge Booster
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.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education