Professional Documents
Culture Documents
LAB 2 - The Structure of C Programs
LAB 2 - The Structure of C Programs
LAB 2 - The Structure of C Programs
=
= =
N N
x x N
N
i
i
N
i
i
sigma
In is given the value of N and the values of measurements (
i
x ).
2) An integer number is said to be perfect number if its factors, including 1 but not the number itself, sum
to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a function perfect that
determines if the parameter number is a perfect number. Use this function in a program that determines
and prints the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm
that the number is indeed perfect. Challenge the power of your computer by testing numbers much
larger than 1000.
3) Write a recursive function power(base, exponent) that when invoked returns base
exponent
.
4) The greatest common divisor (gcd) of a non-negative integer M and a positive integer N can be defined
as follows, where mod(M, N) is the remainder when M is divided by N.
gcd(M, N) = N if mod(M, N)=0
= gcd(N, R) if mod(M, N) = R, where R > 0.
Design and test a recursive function in C called gcd(intM,intN) that computes the GCD of a sequence
of one or more pairs of integers prompted from the user. Recall that the % operator in C computes the
required mod operation. As your program is computing a GCD, it is to have the ability to print out a
new line to the screen to indicate each call to the function gcd. This line is to list the current values of
M and N. This "verbose" mode of operation is to be enabled or disabled when the program first starts
running, in response to a question sent to the user by the main function.