Description
1. A palindrome is a number or a text phrase that reads the same backwards as forwards. For
example, 12321, 3456543, etc. Write a program that reads in a five-digit integer and determines whether it is a palindrome. Hint: use the integer division and modulus operator to
separate the number into its individual digits.
Sample Output:
(i)
Please Enter a 5-Digit Integer :12345
12345 is NOT a palindrome
(ii)
Please Enter a 5-Digit Integer :12321
12321 is a palindrome
2. A person invests $10000.00 in a savings account. Assuming that all interest is left on deposit
in the account, calculate and print the amount of money in the account at the end of 10 years
for interest rates of 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10%. Use the following formula for determining
these amounts:
a = p ∗ (1 + r)
n
(1)
where p is the principal, r is the annual interest rate, n is the number of years, and a is the
amount on deposit at the end of the nth year.
Sample Output:
Rate Amount at the end of 10th year
==== ===============================
0.01 11046.22
0.02 12189.94
0.03 13439.16
0.04 14802.44
0.05 16288.95
0.06 17908.48
0.07 19671.51
0.08 21589.25
0.09 23673.64
0.10 25937.42
3. Pythagorean Triples: A right triangle can have sides that are all integers. A set of three
integer values for the sides of a right triangle is called a Pythagorean triple. These three
sides must satisfy the relationship that the sum of the squares of two of the sides is equal
to the square of the hypotenuse. Find all unique Pythagorean triples for side1, side2 and
hypotenuse, all no longer than 500. How many are there? Note that this problem specifically
asks you to exclude the duplicates, unlike the one on p.167 of the slides. For example, if you
were to check all unique Pythagorean Triples less than 5 (rather than 500) your output must
be
3 4 5
That is, it must NOT output both (3,4,5) and (4,3,5).
Sample Output:
Number of Pythagorean Triples with duplicates : 772
The Innermost for loop is entered 125000000 times.
Number of Pythagorean Triples (no duplicates) : 386
The Innermost for loop is entered 20708500 times.
4. The factorial of a nonnegative integer n is written n! (pronounced ”n factorial”) and is defined
as follows:
n! = n ∗ (n − 1) ∗ (n − 2) ∗ (n − 3) ∗ · · · ∗ 1 (2)
2
0! = 1, 1! = 1 by definition.
For example, 5! = 5*4*3*2*1 = 120
(a) Write a program that reads a nonnegative integer and computes and prints its factorial.
Do not use scientific notation for displaying the result.
(b) Write a program that estimates the value of mathematical constant e by using the
formula:
e = 1 +
1
1! +
1
2! +
1
3! + · · · (3)
Prompt the user for the desired accuracy of e, i.e. the number of terms in the summation.
Use 10 digits of precision to display the result.
Sample Output: (combined for a and b)
Number for Factorial : 16
16! = 20922789888000.000000
Desired Accuracy for “e”(number of terms in the series) : 10
e with 10 terms = 2.7182815256 (with 10 digits of accuracy)
5. Write a program that computes the value of e
x by using the formula
e
x = 1 +
x
1
1! +
x
2
2! +
x
3
3! + · · · (4)
Prompt the user for the desired accuracy of e, i.e. the number of terms in the summation
and the power of ”e”. Use 10 digits of precision to display the result.
Expected Output:
Enter the exponent of “e” : 7
Desired Accuracy (number of terms in the series) : 13
e^7 with 13 terms = 1067.0243116203
6. Write a program that finds and prints all of the prime numbers between 3 and 1000. Print
them and display the total prime numbers between 3 and 1000.
Expected Output:
3 5 7 11 13 17 19 23
29 31 37 41 43 47 53 59
61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137
139 149 151 157 163 167 173 179
181 191 193 197 199 211 223 227
229 233 239 241 251 257 263 269
3
271 277 281 283 293 307 311 313
317 331 337 347 349 353 359 367
373 379 383 389 397 401 409 419
421 431 433 439 443 449 457 461
463 467 479 487 491 499 503 509
521 523 541 547 557 563 569 571
577 587 593 599 601 607 613 617
619 631 641 643 647 653 659 661
673 677 683 691 701 709 719 727
733 739 743 751 757 761 769 773
787 797 809 811 821 823 827 829
839 853 857 859 863 877 881 883
887 907 911 919 929 937 941 947
953 967 971 977 983 991 997
There are 167 prime numbers between 3 and 1000
7. In cryptarithmetic puzzles, mathematical equations are written using letters. Each letter can
be a digit from 0 to 9, but no two letters can be the same. Customarily, distinct letters stand
for different digits Here is a sample problem:
SEND + MORE = MONEY
A solution to the puzzle is S = 9, R = 8, O = 0, M = 1, Y = 2, E = 5, N = 6, D = 7
S E N D
M O R E
+
————
M O N E Y
That is,
9 5 6 7
1 0 8 5
+
————
1 0 6 5 2
Write a program that finds solutions to the following cryptarithmetic puzzle:
TOO + TOO + TOO + TOO = GOOD
T O O
T O O
4
T O O
T O O
+
———–
G O O D
The simplest technique is to use a nested loop for each unique letter (in this case T, O, G,
D). The loops would systematically assign the digits from 0-9 to each letter. For example, it
might first try T = 0, O = 0, G = 0, D = 0, then T = 0, O = 0, G =0, D = 1, then T = 0,
O = 0, G = 0, D = 2, etc. up to T = 9, O = 9, G = 9, D = 9. In the loop body test that
each variable is unique and that the equation is satisfied. Output the values for the letters
that satisfy the equation.
Expected Output:
The values are: T = 1 O = 6 G = 0 D = 4
The values are: T = 4 O = 9 G = 1 D = 6
8. Extra credit (3%): Extend q6 above to get the lower and upper bounds for the range to
look for the prime numbers.
Expected Output:
Lower bound = 1201
Upper bound = 2498
1201 1213 1217 1223 1229 1231 1237 1249
1259 1277 1279 1283 1289 1291 1297 1301
1303 1307 1319 1321 1327 1361 1367 1373
1381 1399 1409 1423 1427 1429 1433 1439
1447 1451 1453 1459 1471 1481 1483 1487
1489 1493 1499 1511 1523 1531 1543 1549
1553 1559 1567 1571 1579 1583 1597 1601
1607 1609 1613 1619 1621 1627 1637 1657
1663 1667 1669 1693 1697 1699 1709 1721
1723 1733 1741 1747 1753 1759 1777 1783
1787 1789 1801 1811 1823 1831 1847 1861
1867 1871 1873 1877 1879 1889 1901 1907
1913 1931 1933 1949 1951 1973 1979 1987
1993 1997 1999 2003 2011 2017 2027 2029
2039 2053 2063 2069 2081 2083 2087 2089
2099 2111 2113 2129 2131 2137 2141 2143
2153 2161 2179 2203 2207 2213 2221 2237
2239 2243 2251 2267 2269 2273 2281 2287
2293 2297 2309 2311 2333 2339 2341 2347
2351 2357 2371 2377 2381 2383 2389 2393
2399 2411 2417 2423 2437 2441 2447 2459
2467 2473 2477
5
There are 171 prime numbers between 1201 and 2498
9. Extra credit: Reduce the number of times the innermost loop is executed to about 1
12 th (or
more maybe) of the normal case, where we go through all the 3-number combinations. The
innermost loop should be executed no more than 10323125 times to get the 10% extra credit.
Further improvement will be 15% additional extra credit.
Expected Output:
Number of Pythagorean Triples with duplicates : 772
The Innermost for loop is entered 125000000 times.
Number of Pythagorean Triples (no duplicates) : 386
The Innermost for loop is entered 20708500 times.
Number of Pythagorean Triples (no duplicates): 386
The Innermost for loop is entered 10323125 times.
Number of Pythagorean Triples (no duplicates) faster one: 386
The Innermost for loop is entered 124750 times.
6
Deliverables:
1. Source Code: (.cpp file) that must start with a comment block similar to the following:
/*************************************************************************
** Author : Suleyman Uludag
** Program : hw1, q1
** Date Created : September 15, 2013
** Date Last Modified : September 16, 2013
** Usage : No command line arguments
**
** Problem:
Accept the following information from the user (keyboard):
– Hw1, hw2 and hw3 (out of 100)
– Midterm (out of 100)
– Final exam (out of 100)
Calculate the total grade out of 100 based on the following grading scale:
Hws –> 30% (10% each)
Midterm –> 30%
Final Exam –> 40%
** Pseudocode:
** 1)
** 2)
*************************************************************************/
2. Executable (.exe file under windows). You must explicitly state the platform of your executable (such as Linux, etc.) if it is not Windows. Please name your file by using the
question number: hw1-q1.exe (for Windows)
3. Screenshot of your app. For screenshot, you can use the following free program on windows:
http://www.wisdom-soft.com/downloads/setupscreenhunterfree.exe
For Linux/Unix, there are many alternatives. I personally like shutter.
File naming convention example:
hw1 q1.png (or .jpg or another graphics format)
4. You must zip all the above three files into ONE .zip file and submit your assignment by the
deadline on moodle system. Name your file as Lastname-Firstname-hw#.zip. For example,
Uludag-Suleyman-hw1.zip
For generating .zip file, you may use the following free software on Windows:
http://www.7-zip.org/download.html
Linux/Unix has many built-in.
7