Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write a C + + program with 3 functions. 1 . The main ( ) function gets an integer from the user using the

Please write a C++ program with 3 functions.
1. The main() function gets an integer from the user using the cin C++ statement.
make sure it's a positive integer. If it's zero or a negative integer loop until you get a positive integer.
which loop is best to use here?
while loop?
do while loop?
for loop?
Once you have a positive integer, call a function called print_evens and pass to it this integer.
2. The print_evens function prints all even numbers from zero to the number it was passed from main()
while it's printing these numbers in a loop, it will call the 3rd function which checks to see if a number is divisible by 8.
the 3rd function will be called divisible_by_8. It will be passed an integer and will return a boolean.
the print_evens function does not return anything.
so you have main() where your program starts running.
you ask the user for an integer in main(). Do some error checking to make sure only positive integers are accepted as input. If the user types -5 then you should print a message saying "Please input a positive integer" and loop back.
Then you call the print_evens function and pass it this positive integer.
print_evens will print all the even numbers starting from 0 up to this positive integer.
print_evens will call the 3rd function called divisible_by_8 and pass it each even number and this function will return true if the number is divisible by 8 and false if it's not.
You can have an if statement in print_evens like this:
if (divisible_by_8)
// print that number is divisible by 8
__________________________________________________
a sample run can look like this:
Please input a positive integer: -5
That wasn't a positive integer, please input a positive integer: 20
0
0 is divisible by 8!
2
4
6
8
8 is divisible by 8!
10
12
14
16
16 is divisible by 8!
18
20Please write a C++ program with 3 functions.
1. The main() function gets an integer from the user using the cin C++ statement.
make sure it's a positive integer. If it's zero or a negative integer loop until you get a positive integer.
which loop is best to use here?
while loop?
do while loop?
for loop?
Once you have a positive integer, call a function called print_evens and pass to it this integer.
2. The print_evens function prints all even numbers from zero to the number it was passed from main()
while it's printing these numbers in a loop, it will call the 3rd function which checks to see if a number is divisible by 8.
the 3rd function will be called divisible_by_8. It will be passed an integer and will return a boolean.
the print_evens function does not return anything.
so you have main() where your program starts running.
you ask the user for an integer in main(). Do some error checking to make sure only positive integers are accepted as input. If the user types -5 then you should print a message saying "Please input a positive integer" and loop back.
Then you call the print_evens function and pass it this positive integer.
print_evens will print all the even numbers starting from 0 up to this positive integer.
print_evens will call the 3rd function called divisible_by_8 and pass it each even number and this function will return true if the number is divisible by 8 and false if it's not.
You can have an if statement in print_evens like this:
if (divisible_by_8)
// print that number is divisible by 8
__________________________________________________
a sample run can look like this:
Please input a positive integer: -5
That wasn't a positive integer, please input a positive integer: 20
0
0 is divisible by 8!
2
4
6
8
8 is divisible by 8!
10
12
14
16
16 is divisible by 8!
18
20

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions