Practice Program Assignment 3
1. A Smith number is a composite number, whose sum of the digits is equal to the sum
of its prime factors. For example: 4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.
Write a program in Java to enter a number and check whether it is a Smith number
or not.
Sample Input: 666
Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18.
Thus, 666 is a Smith Number.
2. A Goldbach number is a positive even integer that can be expressed as the sum of
two odd primes. It is to be noted that all even integer numbers greater than 4 are
Goldbach numbers.
Example:
6=3+3
10 = 3 + 7
10 = 5 + 5
Hence, ‘6’ has one odd prime pair ‘3’ and ‘3’. Similarly, ‘10’ has two odd prime pairs,
i.e. ‘3’ and ‘7’, ‘5’ and ‘5’.
Write a program to accept an even integer 'N' where N > 9 and N < 50. Find all the
odd prime pairs whose sum is equal to the number 'N'.
Test your program with the following data and some random data:
Example 1 Example 2
INPUT: INPUT:
N = 14 N = 30
OUTPUT: OUTPUT:
PRIME PAIRS ARE: PRIME PAIRS ARE:
3, 11 7, 23
7, 7 11, 19
13, 17
3. Input a sentence from the user and count the number of times, the words “an” and
“and” are present in the sentence. Design a class Frequency using the description
given below:
Class name : Frequency
Data Members
text stores the sentence
countand to store the frequency of the word ‘and’.
countan to store the frequency of the word ‘an’.
len stores the length of the string
Member functions
Frequency() constructor to initialize the data variables.
void accept(String n) to assign n to text where the value of the
parameter should be in lowercase.
void checkandfreq() to count the frequency of ‘and’.
void checkanfreq() to count the frequency of ‘an’.
void display() to display the frequency of ‘an’ and ‘and’
with suitable messages.
Specify class Frequency giving details of the constructor(), void accept(String), void
checkand freq() and void display(). Also define the main function to create an object
and call methods accordingly to enable the task.