CS1101C Lab 2 (Even Week)

Square Numbers

The deadline for this lab question is Friday 09 March 2007, 23:59:59 hours.

The name of your C program file must be called square1.c, files with any other name will not be marked.

Preliminary

Every square number can be written as the form:

n * n

for some natural number n. The sequence of square numbers for n = 1, 2, 3... is:

1, 4, 9, 16, 25, 36, 49, 64, 81, 100, ...

Your Task

Write a program called square1.c that prompts the user to input an integer x. If x is a square number, display it. If x is not a square number, display the nearest square number below x and the nearest square number above x.

Keep prompting the user to enter an integer until the user chooses to quit the program by entering a value of 0. When the user quits the program, display the number of integers entered, as well as the number of square and non-square numbers.

Sample Runs

Assuming that the executable is square1, a sample run of the program is shown below, which you must follow precisely. User input is denoted in bold.

$ gcc -Wall square1.c -o square1
$ ./square1
Enter an integer (1 - 2000000000, 0 to quit): 1
1 is a square number.

Enter an integer (1 - 2000000000, 0 to quit): 2
2 is between square numbers 1 and 4.

Enter an integer (1 - 2000000000, 0 to quit): 97
97 is between square numbers 81 and 100.

Enter an integer (1 - 2000000000, 0 to quit): 49
49 is a square number.

Enter an integer (1 - 2000000000, 0 to quit): 50
50 is between square numbers 49 and 64.

Enter an integer (1 - 2000000000, 0 to quit): 1999967841
1999967841 is a square number.

Enter an integer (1 - 2000000000, 0 to quit): 2000000000
2000000000 is between square numbers 1999967841 and 2000057284.

Enter an integer (1 - 2000000000, 0 to quit): 0
Number of integers entered: 7
Square numbers: 3
Non-square numbers: 4
Thank you for using my program. Goodbye!

$

Points to Note


This document, index.html, has been accessed 21 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 10 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 11 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.