Lab 04
Lab 04
Lab 04
Installing
Problem 1 – Squares
Write a program that prompts the user for an integer n, and then prints out all even squares greater
than 1 and less than or equal to n. A sample run of the program might look like this (user input is in
italics).
Squares are:
2^2 = 4
4^2 = 16
6^2 = 36
8^2 = 64
10^2 = 100
Implement your program in your squares.c file. To compile, type gcc -Wall -o squares squares.c,
and to run, type squares. Remember to fill in the header and to properly comment your program.
Problem 2 - Reverse
Write a program that takes in an arbitrary-length number (1 or more digits) and reverses it. You
may not ask the user for length of the number; you must simply take in an integer and spit it back
out with its digits reversed.
Hint: Think about a kind of loop that will allow you to DO something, and then continue
to do it WHILE a certain condition is true.
Implement your program in your reverse.c file. To compile, type gcc -Wall -o reverse
reverse.c, and type reverse to run. As always, remember to fill in the header and to
properly comment your code.
Problem 3 – Numbers
Write a program that reads in an arbitrary-length series of digits separated by white space and then
prints out the digits as one, complete number. The user will signify the end of the string of digits by
entering a negative number. One run of the program might look like this:
NOTE: Within your program, you must turn the single digits into a single integer. You cannot
just print out the digits with no space between them.
Warning: This program is tougher than it looks. Implement your program in the num.c file.
Compile using gcc -Wall -o num num.c, and run by typing num.
A perfect number is a number which equals the sum of its proper divisors (also called aliquot
parts). A proper divisor that divides the number evenly, but is not the number itself.
In order to accomplish this task, write a function that computes the sum of all proper divisors of a
given number. This function can be called in a for loop cycling from 1 to the specified upper limit.
6 = 1 + 2 + 3,
28 = 1 + 2 + 4 + 7 + 14,
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064
How far can you get? Implement your program in a file perfect.c.
Problem 5 – Random Permutations
Write a program that will read an array of M numbers - M being a pre-defined constant – and
outputs 5 random permutations (shufflings) of these numbers. (Which means for each shuffling you
have to re-arrange the numbers in some random order.)
Hint: You should use the rand() function of the Standard C Library to implement
randNumber(int). (Look at Appendix D in your book. It details the Standard
Library.) Remember to include stdlib.h so you have access to the Standard Library functions.
#include <stdlib.h>
Implement your functions in your perm.c file. In your main() function, write some code that will
read a sequence of 10 integers and outputs a random permutation.