Question: create a C program and save it as integerPropsYourInitials.c in your cop 2 2 2 0 folder. ( Replace YourInitials with your own initials. )
create a C program and save it as integerPropsYourInitials.c in
your cop folder. Replace YourInitials with your own initials.
The program is designed to determine which integers from to a given
integer are prime or perfect. The program will be designed so that it
contains functions that you will write. ALL function bodies are to be
written BELOW the main function.
The function prototypes that you type just above the header of the
main function are: Note that you will be writing the code for these
functions below the main.
int getUpperBound;
int isPrimeint;
int isPerfectint;
The algorithm for the main function follows: Pay attention to
indentation in the algorithm.
Declare an integer variable named upperBound.
Call a function that YOU will write later named getUpperBound
that takes no parameters but returns an integer that you assign
to the upperBound variable.
Declare a variable named num.
Write a for loop that starts num at and goes up to and
including the upperBound. Increment num by In the loop do:
Use a function that you will write named isPrime to see
if num is a prime number. The function should be passed
the variable num. If isPrime returns then print the
number along with a message that says that the number is a
prime number.
Integer Properties
Page
Else, use a function that you will write named isPerfect
to see if num is a perfect number. The function should be
passed the variable num. If isPerfect returns then
print the number along with a message that says that the
number is a perfect number.
The algorithm for the getUpperBound function follows:
Declare an integer variable named upper.
Do
Ask for an integer bigger than or equal to and less than
or equal to and store that integer in the upper
variable.
If upper is less than or bigger than print a
message stating that the integer must be from to
while upper is less than or bigger than
Return upper.
The algorithm for the isPrime function follows:
A prime number is an integer bigger than that only has and
itself as divisors.
As shown in the prototype near the top of this document, this
function takes one parameter that I will refer to as the
parameter. You must give your parameter an actual name NOT
parameter
See if the parameter is less than or equal to If it is
return
See if the parameter is equal to If it is return
Declare an integer variable named divisor.
Start a for loop by setting divisor equal to and going up to
but not including, the parameter. Increment the divisor by
Inside the loop do:
See if the parameter is divisible by the divisor. Note:
an integer m is divisible by another integer n if m mod n
is That is the remainder upon division of m by n is
If it is return
Outside and below the loop, return
The algorithm for the isPerfect function follows:
A perfect number is an integer that is equal to the sum of its
divisors excluding the number itself. For example: is perfect
because is perfect because it is equal to
Integer Properties
Page
As shown in the prototype near the top of this document, this
function takes one parameter that I will refer to as the
parameter. You must give your parameter an actual name NOT
parameter
See if the parameter is less than or equal to If it is
return
Declare an integer variable named total and set it to
Declare an integer variable named divisor.
Start a for loop by setting divisor equal to and going up to
but not including, the parameter. Increment the divisor by
Inside the loop do:
See if the parameter is divisible by the divisor. If it
is add the divisor to the total.
Outside and below the loop, check to see if the total is equal
to the parameter. If it is return
Else Return
If you test your program by entering when you are asked for an
integer, you should see output like the following:
is prime.
is prime.
is prime.
is perfect.
is prime.
is prime.
is prime.
is prime.
is prime.
is prime.
is perfect.
is prime.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
