Loop Review Exercises

1. Exercise PrintTriangles: Write a program to print one of the following patterns using nested-loops(pattern of your choice). Write the main() which prompts the user for the numRows and prints the pattern.

1

1 2 1

1 2 4 2 1

1 2 4 8 4 2 1

1 2 4 8 16 8 4 2 1

1 2 4 8 16 32 16 8 4 2 1

1 2 4 8 16 32 64 32 16 8 4 2 1

1 2 4 8 16 32 64 128 64 32 16 8 4 2 1

(a) PowerOf2Triangle

1 1

1 1 1 1

1 2 1 1 2 1

1 3 3 1 1 3 3 1

1 4 6 4 1 1 4 6 4 1

1 5 10 10 5 1 1 5 10 10 5 1

1 6 15 20 15 6 1 1 6 15 20 15 6 1

(b) PascalTriangle1 (c) PascalTriangle2

2.  Largest number of divisors:

Which integer between 1 and 10000 has the largest number of divisors, and how many divisors does it have? Write a program to find the answers and print out the results. Keep in mind that it is possible that several integers in this range have the same, maximum number of divisors.

3. Caesar’s Cipher

In cryptography, a Caesar's cipher is one of the simplest and most widely-known encryption techniques. To code the message each letter in the plaintext is replaced by a letter some number of positions further down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. If the end of the alphabet is reached, the letters should start from the beginning: X would become A, Y would become B and Z would become C.

Coding:

AàD; BàE; CàF; DàG; .... WàZ; XàA; YàB; ZàC

Decoding:

DàA; E àB; FàC; GàD; .... ZàW; AàX; BàY; CàZ

Any non-letters (like spaces, dots, comas and so on) will not change.

For example, DEVROXWHOB!! can be decoded as: ABSOLUTELY!!

That code can be easily broken if we know the shift value.

You are a spy who must use to computer to communicate with your spy partner and decode secret messages. You will be given two messages. The first input will contain a message that was encrypted using the Caesar Cipher. It will contain letter and punctuation (like spaces, dots, comas and so on). If the character is a letter, it will be upper case. The second input will be the decrypted first word of the encrypted message. You can use this word to find out the shift, used by your partner.

Sample Input #1

KHOOR WKHUH.

HELLO

Sample Output #1

HELLO THERE.

------

Sample Input #2

JKIKSHKX OY BKXXXXXXE IURJ....

DECEMBER

Sample Output #2

DECEMBER IS VERRRRRRY COLD....

------

Sample Input #3

CAH RC. HXD'UU URTN RC.

TRY

Sample Output #3

TRY IT. YOU’LL LIKE IT.

4. Alphabetically Speaking

a) Write a program that accepts a sentence of text and reorders the letters within the sentence into alphabetical order according to the following rules:(You may assume that your sample data is a sentence not over 40 characters long.)

Only the letters from A-Z are affected.

The output consists of the original sentence displayed on the screen with the output directly below it.

SAMPLE INPUT #1
THE PRICE OF BREAD IS $1.25 PER POUND
SAMPLE OUTPUT #1
THE PRICE OF BREAD IS $1.25 PER POUND
ABCDDEEEEFHIINOOPPPRRRSTU / SAMPLE DATA FILE #2:
THE LICENCE PLATE READ G76-ZA3
SAMPLE OUTPUT #2 (on a cleared screen):
THE LICENCE PLATE READ G76-ZA3
AAACCDEEEEEGHILLNPRTTZ

b) The same as question a), with the following condition:

The sorted output reflects the format of the original sentence with all non-alphabetic characters in their original positions.

SAMPLE INPUT #1:
THE PRICE OF BREAD IS $1.25 PER POUND
SAMPLE OUTPUT #1:
THE PRICE OF BREAD IS $1.25 PER POUND
ABC DDEEE EF HIINO OP $1.25 PPR RRSTU / SAMPLE INPUT #2:
THE LICENCE PLATE READ G76-ZA3
SAMPLE OUTPUT #2:
THE LICENCE PLATE READ G76-ZA3
AAA CCDEEEE EGHIL LNPR T76-TZ3