You are on page 1of 3

CS 153 Program 3 Sums of Squares

(edits: Aug 17, 2015).

1. Write a program that finds and writes out all pairs of positive integers x
and y such that x2 + y2 = z. Prompt the user for the value z. Of course, x
and y may either or both be zero.
For example, if z is 25, then x and y are:
0, 5; 3, 4; 4, 3; 5, 0.
If no such x and y are found, write out None Found. Dont search through
all possible integers to find x and y. For a given value of z you can stop
searching for x when x2 > z. (And the same for y.)
Expect z to be zero up to INT_MAX. Remind the user of this when the
program starts. Include the file limits.h at the top of your program.
Assume that the user has entered characters that designate a positive
integer less than or equal to INT_MAX. (It is hard to test if what the user
entered is more than this, so for this program just assume that z is
correct.)*
Write the program as a main(). Play around with your main() to be sure
that it works correctly.

2. Say that a users PIN consists of 4 digits and you know that the digits of
the PIN add up to 6. Write a program that writes out all PINs that match this
condition. Different permutations of the same digits count as different PINs,
of course. So 1230 is a different pin than 1032.
3. An evil number is a positive integer with an even number of 1s in its
binary expansion. For example, 3 is evil because it has two 1s in its
expansion. 15 is evil because its expansion is 1111. Zero counts as evil.
Of course, when a 32-bit int represents 15 it is
00000000000000000000000000001111
but it is still evil. The first several evil numbers are
0, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20, 23 . . .
Write a program that asks the user for N and writes out all evil integers zero
up to N. Expect N to be zero up to INT_MAX. The C right-shift operator may
be useful. There may be clever ways to use number theory to do this, but
just do it in the obvious straightforward way.

Turn In: One source file, with three different mains, named main01,
main02, and main03.
Compile and test your programs using MS-VC++ . Be sure that you are
using ANSI C.
Turn in a source file containing the program. Do not submit a full project or
a zip file. Be sure that what you turn in is a source file. Use the "Add
Attachments" button to do this. Neatness counts when these programs are
graded.

*It is hard to test for out-of-range entries because without some advanced fiddling, scanf
will read characters and convert them into bits, which are put into a 32-bit int. The int
cant possibly be anything other than what can be held in an int, but might not correspond
to what the user typed since there is nothing to stop the user from typing in the digits of a
very large integer.
You might think a clever way to avoid this problem is to read what the user typed into a
long, and then check that the value in the long is less than or equal to INT_MAX. But
officially, a long and an int might be the same size, so this might not work.
Another way might be to read the characters into a double, then check the double against
INT_MAX. The best solution is to read the characters into a character buffer and inspect
them before converting to an int, but that is too much work for this assignment.
Making a program user-friendly often involves more programming than the rest of the
program.

You might also like