CS1101C Lab 2 (Odd Week)

Triangular Numbers

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

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

Preliminary

Every triangular number can be written as the sum:

1 + 2 + 3 + ... + n

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

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Your Task

Write a program called triangular1.c that prompts the user to input an integer x. If x is a triangular number, display it. If x is not a triangular number, display the nearest triangular number below x and the nearest triangular 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 triangular and non-triangular numbers.

Sample Runs

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

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

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

Enter an integer (1 - 2000000000, 0 to quit): 97
97 is between triangular numbers 91 and 105.

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

Enter an integer (1 - 2000000000, 0 to quit): 46
46 is between triangular numbers 45 and 55.

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

Enter an integer (1 - 2000000000, 0 to quit): 2000000000
2000000000 is between triangular numbers 1999996635 and 2000059881.

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

$

Points to Note


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

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

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