Answered step by step
Verified Expert Solution
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 functions.
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 printevens and pass to it this integer.
The printevens 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 rd function which checks to see if a number is divisible by
the rd function will be called divisibleby It will be passed an integer and will return a boolean.
the printevens 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 then you should print a message saying "Please input a positive integer" and loop back.
Then you call the printevens function and pass it this positive integer.
printevens will print all the even numbers starting from up to this positive integer.
printevens will call the rd function called divisibleby and pass it each even number and this function will return true if the number is divisible by and false if it's not.
You can have an if statement in printevens like this:
if divisibleby
print that number is divisible by
a sample run can look like this:
Please input a positive integer:
That wasn't a positive integer, please input a positive integer:
is divisible by
is divisible by
is divisible by
Please write a C program with functions.
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 printevens and pass to it this integer.
The printevens 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 rd function which checks to see if a number is divisible by
the rd function will be called divisibleby It will be passed an integer and will return a boolean.
the printevens 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 then you should print a message saying "Please input a positive integer" and loop back.
Then you call the printevens function and pass it this positive integer.
printevens will print all the even numbers starting from up to this positive integer.
printevens will call the rd function called divisibleby and pass it each even number and this function will return true if the number is divisible by and false if it's not.
You can have an if statement in printevens like this:
if divisibleby
print that number is divisible by
a sample run can look like this:
Please input a positive integer:
That wasn't a positive integer, please input a positive integer:
is divisible by
is divisible by
is divisible by
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started