You are on page 1of 2

Practice Program Assignment 4

1. An Armstrong number is a positive m-digit number that is equal to the sum of the mth
powers of their digits. For example:

153: 13 + 53 + 33 = 1 + 125+ 27 = 153


1634: 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643

A Pronic number is a number which is the product of two consecutive integers, that is, a
number of the form n(n + 1). For example:

6= 2 x 3
12 = 3 x 4
20 = 4 x 5

Write a program in Java that will accept two positive integers ‘p’ and ‘q’, such that p < q and
as per user’s choice, will find out, within ‘p’ and ‘q’,

i. all the Armstrong numbers along with their frequency


ii. all the Pronic numbers along with their frequency

It is to be noted that

a. Your program should print an error message and will ask the user to make
choice between trying again or to exit, if by mistake he/she picks up a value for
‘q’ which happens to be less than ‘p.

b. Even if inputs are fine, it will not exit after producing the output, until the user
allows it to.

2. A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well
as an Adam number.

Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 … etc.

Adam number: The square of a number and the square of its reverse are reverse to each
other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
So, 13 is an Adam number.
Write a program in Java that will accept two positive integers m and n, where m is less than n
as user input. Display all Prime-Adam integers that are in the range between m and n (both
inclusive) and output them along with the frequency, in the format given below:

Test your program with the following data and some random data:

Example 1 Example 2

INPUT: INPUT:
m=5 m = 100
n = 100 n = 200

OUTPUT: OUTPUT:
THE PRIME-ADAM INTEGERS ARE: THE PRIME-ADAM INTEGERS ARE:
11 13 31 101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3 FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

3. The computer department of the Agency of International Espionage is trying to


decode Intercepted messages. The agency’s spies have determined that the enemy
encodes Messages by first converting all characters to their ASCII values and then reversing
the string.
For example, consider A_z (the underscore is just to highlight the space). The ASCII values of
A_z are 65, 32, and 122 respectively. Concatenate them to get 6532122 and then reverse
this to get 2212356 as the coded message.
Write a program which reads a coded message and decodes it. The coded message will not
exceed 200 characters. It will contain only alphabets (A …… Z and a …….z) and spaces. ASCII
values of A - Z are 65 - 90 and those of a - z are 97 - 122.

Test your Program for the following data and some random data.
SAMPLE DATA:
INPUT:
Encoded Message:
2312179862310199501872379231018117927
OUTPUT:
THE DECODED MESSAGE:           Have a Nice Day
INPUT:
Encoded Message:
23 5 1 1 0 1 1 5 0 1 7 8 2 3 5 1 1 1 2 1 7 9 9 1 1 8 0 1 5 6 2 3 4 0 1 6 1 1 7 1 1 4 1 1 4 8
 OUTPUT:
THE DECODED MESSAGE:           Truth Always Wins

You might also like