Computer Assignment
Computer Assignment
a. 192 x 1 = 192
b. 192 x 2 = 384
c. 192 x 3 = 576
Concatenating the results: 192 384 576 .It could be observed that
'192384576' consists of all digits from 1 to 9 exactly once. Hence, it could be
concluded that 192 is a Fascinating Number. Some examples of fascinating
Numbers are: 192, 219, 273, 327, 1902, 1920, 2019 etc.
Algorithm
Step 1: Start
Step 3: Check if the number has 3 or more digits (i.e., n >= 100)
If not, terminate the program or skip checking.
1 | Page
Step 8: Sum all digits of the string str
Let sum = 0
For each character ch in str, convert to digit and add to sum
Program:
2 | Page
3 | Page
Output
4 | Page
Question 2
Algorithm
Step 1: Start
Step 7: End
Program
5 | Page
Output
Question 3
6 | Page
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.
Adam number: The square of a number and the square of its reverse are
reverse to each other.
(13)2 = 169
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 INPUT:
m=5
n = 100
Algorithm
7 | Page
c) Else, run a loop from j = 2 to j ≤ √i:
- num = num / 10
b) Compute SquOri = i * i.
- temp = temp / 10
Program
8 | Page
9 | Page
10 | P a g e
Output
11 | P a g e
Question 4
Given two integers m and n, where m < n, write a program to determine how
many prime-palindrome integers are there in the range between m and n
(both inclusive) and output them. The input contains two positive integers m
and n where m < 3000 and n < 3000. Display the number of prime-
palindrome integers in the specified range along with their values in the
format specified below:
Test your program with the sample data and some random data:
Example 1
INPUT:
m = 100
N = 1000
OUTPUT:
101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929
Example 2
INPUT:
m = 100
n = 5000
Algorithm
2) Prompt the user to enter a value for m such that 0 < m < 3000.
12 | P a g e
4) If m is not in the valid range, display "The number is out of range" and
stop the program.
5) Prompt the user to enter a value for n such that 0 < n < 3000 and m < n.
7) If n is not in the valid range, display "The number is out of range" and stop
the program.
23) After the loop ends, print "The number of prime palindrome are "
followed by the value of palc.
13 | P a g e
Program
14 | P a g e
15 | P a g e
Output
Question 5
16 | P a g e
Write a program to input a number and check whether it is 'Magic Number' or
not. Display the message accordingly.
Sample Input : 55
Then, 5 + 5 = 10, 1 + 0 = 1
Algorithm
5) Initialize sum = 0.
8) Add the last digit of temp to sum (sum += temp % 10), then update
temp = temp / 10.
Program
17 | P a g e
Output
18 | P a g e
Question 6
A number is said to Bouncy number if the digits of the number are unsorted.
19 | P a g e
For example,
22344 - It is not a Bouncy number because the digits are sorted in ascending
order.
Algorithm
6) If not, print "The number is out of range" and end the program.
11) If a equals the length of the number, print "The number is not bouncy"
(ascending).
12) Else if d equals the length of the number, print "The number is not
bouncy" (descending).
20 | P a g e
Program
21 | P a g e
Output
22 | P a g e
Question 7
A Circular Prime is a prime number that remains prime under cyclic shifts of
its digits. When the leftmost digit is removed and replaced at the end of the
remaining string of digits, the generated number is still prime. The process is
repeated until the original number is reached again.
Example:
131
311
113
Test your program with the following data and some random data:
Example 1
INPUT:
N = 197
OUTPUT:
197
971
719
Algorithm
23 | P a g e
4) Convert n to a string and store it in variable nn.
8) Else:
12) If n % j == 0, increment c.
n = r * (10^(length of nn - 1)) + q
24 | P a g e
Program
25 | P a g e
Output
Question 8
26 | P a g e
An Evil number is a positive whole number which has even number of 1's in
its binary equivalent. Example: Binary equivalent of 9 is 1001, which
contains even number of 1's. A few evil numbers are 3, 5, 6, 9…. Design a
program to accept a positive whole number and find the binary equivalent of
the number and count the number of 1's in it and display whether it is a Evil
number or not with an appropriate message. Output the result in format
given below:
Example 1
Input: 15
No. of 1's: 4
Example 2
Input: 26
No. of 1's: 3
Algorithm:
27 | P a g e
10) If count % 2 == 0:
12) Else:
Program
Output
28 | P a g e
Question 9
29 | P a g e
Write a program to input two valid dates, each comprising of Day (2 digits),
Month (2 digits) and Year (4 digits) and calculate the days elapsed between
both the dates.
FIRST DATE:
Day: 24
Month: 09
Year: 1960
SECOND DATE:
Day: 08
Month: 12
Year: 1852
Output: xxxxxxxx
Algorithm
1. Start
30 | P a g e
11. Display "Number of days elapsed between the two dates: " + BetDays
12. End
Program
Output
31 | P a g e
Question 10
32 | P a g e
Design a program to accept a day number (between 1 and 366), year (in 4
digits) from the user to generate and display the corresponding date. Also,
accept 'N' (1 <= N <= 100) from the user to compute and display the future
date corresponding to 'N' days after the generated date. Display an error
message if the value of the day number, year and N are not within the limit
or not according to the condition specified.
Test your program with the following data and some random data:
Example 1
INPUT:
YEAR: 2018
OUTPUT:
Algorithm
1) Start
3) Compute leap:
5) If (d < 1 OR d > max OR n < 1 OR n > 100), print "Invalid input!" and stop
6) Create array days = {31, (leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31,
30, 31}
7) Create array m =
{"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","S
EPTEMBER","OCTOBER","NOVEMBER","DECEMBER"}
33 | P a g e
8) Set mon = 0
- d = d - days[mon]
- mon = mon + 1
11) d = d + n
- d = d - days[mon]
- mon = mon + 1
- mon = 0
-y=y+1
14) End
34 | P a g e
Program
35 | P a g e
Output
Question 11
36 | P a g e
Design a class Convert to find the date and the month from a given day
number for a particular year.
Example: If day number is 64 and the year is 2020, then the corresponding
date would be: March 4,
2020 i.e. (31 + 29 + 4 = 64). Some of the members of the class are given
below:
Methods/Member functions:
Convert(): constructor to initialize the data members with legal initial values.
void dayToDate(): converts the day number to its corresponding date for a
particular year and stores the date in ‘d’ and the month in ‘m’.
Specify the class Convert giving details of the constructor, void accept(), void
dayToDate() and void display(). Define a main() function to create an object
and call the functions accordingly to enable the task.
Algorithm
1) Start
- n (day number)
- d (day of month)
- m (month number)
- y (year)
37 | P a g e
3) Define constructor Convert():
- Initialize n, d, m, y = 0
- Read year y
- Set m = 0
- n = n – days[m]
-m=m+1
- Set d = n
- Set m = m + 1
7) In main():
- Call obj.accept()
- Call obj.dayToDate()
- Call obj.display()
8) End
Program
38 | P a g e
Output
39 | P a g e
Question 12
40 | P a g e
Write a program to accept a date in the string format dd/mm/yyyy and
accept the name of the day on 1st of January of the corresponding year. Find
the day for the given date.
Example:
Input:
Date: 5/7/2001
Output:
Algorithm
1. Start
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
Set daysInMonth[1] = 29
END IF
7. Calculate totalDays:
- Set totalDays = 0
41 | P a g e
totalDays = totalDays + daysInMonth[i]
- END FOR
9. Find startIndex:
- FOR i = 0 to 6
startIndex = i
BREAK
END IF
- END FOR
11. Output:
12. End
Program
42 | P a g e
Output
43 | P a g e
Question 13
44 | P a g e
Write a program that reads a string and a character. Each occurrence of the
given character (regardless of its case) is converted to opposite case.
OUTPUT: aAbracAdAbra ( see each ‘a’ or ‘A’ is converted to opposite case ie.
upper to lower or vice-versa)
Algorithm
Step 1: Start.
Step 7: Stop.
Program
45 | P a g e
Output
46 | P a g e
Question 14
47 | P a g e
Write a program to Input a word in uppercase and check for the position of
the first occurring vowel and perform the following operation.
(i) Words that begin with a vowel are concatenated with “Y”.
(ii) Words that contain a vowel in-between should have the first part from the
position of the vowel till the end, followed by the part of the string from
beginning till the position of the vowel and is concatenated by “C”.
(iii) Words which do not contain a vowel are concatenated with “N”.
Algorithm
1. Start
4. Set pos = -1
5. For i = 0 to word.length - 1 do
pos = i
Break
6. If pos == 0 then
p2 = substring(word, 0, pos)
48 | P a g e
result = p1 + p2 + "C"
8. Else
9. Print result
10. Stop
Program
Output
49 | P a g e
Question 15
50 | P a g e
A class TheString accepts a string of a maximum of 100 characters with only
one blank space between the words. Some of the members of the class are
as follows:
Member functions/methods:
void Display( ) : to display the original string, along with the number of words
and the number of consonants
Specify the class TheString giving the details of the constructors, void
countFreq() and void Display( ). Define the main( ) function to create an
object and call the functions accordingly to enable the task.
Algorithm
1. Start
3. Define constructors:
51 | P a g e
Parameterized constructor: assign str = ds
Initialize wordcount = 1
Initialize cons = 0
o Convert to lowercase
Call countFreq()
Call Display()
7.End
Program
52 | P a g e
Output
53 | P a g e
Question 16
54 | P a g e
Class name : ConsChange
Member functions/methods:
void shiftcons( ) : shifts all the consonants of the word at the beginning
followed by the vowels (e.g. spoon becomes spnoo)
void show( ) : displays the original word, shifted word and the changed word
Specify the
Algorithm
1.Start
3.Constructor (ConsChange())
Set len = 0
4.Function readword()
55 | P a g e
Input a word from the user in lowercase.
Store it in word.
5.Function shiftcons()
6.Function changeword()
Initialize cw = "".
7.Function show()
Call ob.readword().
Call ob.shiftcons().
Call ob.changeword().
Call ob.show().
56 | P a g e
9.End
Program
57 | P a g e
58 | P a g e
Output
Question 17
59 | P a g e
Write a program to accept a sentence as input. The words in the string are to
be separated by a blank. Each word must be in upper case. The sentence is
terminated by either “.”,”!” or “?”. Perform the following tasks:
Test your program with the sample data and some random data:
Example 1:
OUTPUT:
Length: 6
Rearranged Sentence:
Algorithm
Step 1: Start
Step 4: Split the remaining sentence into individual words using space " " as
a delimiter.
Step 6: Sort the array of words in alphabetical order using the following
process:
a. For each word position i from 0 to length - 2:
- Compare words[j] with words[j+1].
- If words[j] is greater (comes later in dictionary order), swap them.
60 | P a g e
Step 9: End
Program
Output
61 | P a g e
Question 18
62 | P a g e
Write a program to accept a sentence which may be terminated by either '.',
'?' or '!' only. The words are to be separated by a single blank space and are
in UPPER CASE.
Check for the validity of the accepted sentence only for the terminating
character.
Arrange the words in ascending order of their length. If two or more words
have the same length, then sort them alphabetically.
Test your program for the following data and some random data:
Example 1:
INPUT:
OUTPUT:
Example 2:
INPUT:
OUTPUT:
Algorithm
1.Start
63 | P a g e
3.Check validity of sentence termination
6.Split the sentence into words using " " (space) as a delimiter.
Secondary key: If two words have the same length, arrange them
alphabetically.
10.End
64 | P a g e
Program
65 | P a g e
Output
66 | P a g e
Question 19
You can assume that no word in the sentence exceeds 15 characters, so that
you get a proper formatted output.
(ii) Find the number of vowels and consonants in each word and display them
with proper headings along with the words.
Example 1
OUTPUT:
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
OUTPUT:
God Is Great.
God 1 2
Is 1 1
67 | P a g e
Great 2 3
Example 3
OUTPUT:
Invalid Input
Algorithm
1. Start
7. Split the sentence into words using a single space as the delimiter.
a. Capitalize the first letter of the word and make the rest lowercase.
68 | P a g e
- IF `ch` is 'a', 'e', 'i', 'o', or 'u' THEN increment `vowels`.
c. Print the word, vowels count, and consonants count in tabular format.
13. End
Program
69 | P a g e
70 | P a g e
Output
Question 20
71 | P a g e
Write a program to print the initials of string recursively
A class Initials Print has been defined to print the initials of the name. Some
of the members of the class are given below:
Member functions:
void initRecursive(String s, int i): to store the initials of the name in the
variable initials as shown in the sample output using Recursive technique.
display(): to accept full name from the user and print the initials of string as
shown in the sample output.
Specify the class Initials Print the initials of string giving details of the
constructor, void initRecursive (String s, int i) and void display (). Write main
() to create and object and call the respective methods.
Algorithm
1.Start
Data member:
72 | P a g e
Else if:
4.Inside display():
6.End
Program
73 | P a g e
Output
74 | P a g e
Question 21
75 | P a g e
Write a program to accept a sentence which may be terminated by either ‘.’
or ‘?’ or ‘!’ only. Any other character may be ignored. The words may be
separated by more than one blank space and are in uppercase.
(a) Accept a sentence and remove all the extra blank space between two
words to a single blank space.
(b) Accept any word from the user along with its position and insert the word
in the given position. The position is calculated by place value of each word
where first word is in position 1, second word in position 2 and so on.
Algorithm
1.Start
76 | P a g e
If the index = position - 1, insert the new word first.
5.End
Program
77 | P a g e
Output
78 | P a g e
79 | P a g e
Question 22
(a) Check for the validity of the accepted paragraph for the number of
sentences and for the
terminating character.
(b) Separate the two sentences from the paragraph and find the common
words in the two sentences with their frequency of occurrence in the
paragraph.
(c) Display both the sentences separately along with the common words and
their frequency, in the format given below.
Test your program for the following data and some random data:
EXAMPLE
INPUT:
OUTPUT:
IS IT RAINING?
IS 2
IT 2
RAINING 2
80 | P a g e
Algorithm
Step 1: Start.
If more than two sentences are found → print "Invalid Input" and stop.
Step 6: Remove the punctuation marks from the end of both sentences.
Step 7: Break both sentences into words using a tokenizer (split by spaces).
For each word in arr1, count how many times it matches with words in
arr2.
81 | P a g e
Program
82 | P a g e
83 | P a g e
Output
84 | P a g e
Question 23
(a) Find the number of words beginning and ending with a vowel.
(b) Place the words which begin and end with a vowel at the beginning,
followed by the remaining words as they occur in the sentence.
Test your program for words that start and end with a vowel with the sample
data and some random data: words that start and end with a vowel
Example 1
Algorithm
1. Start
2. Input a sentence from the user (must be in uppercase and end with ., ?,
or !).
3. Check validity:
4. Remove punctuation:
Remove the last character from the sentence for further processing.
6. Initialize variables:
85 | P a g e
c = 0 (counter for words starting and ending with vowels).
o Increment c by 1.
8. Output results:
9. End
Program
86 | P a g e
87 | P a g e
Output
88 | P a g e
Question 24
1,2,3,4
4,1,2,3
3,4,1,2
2,3,4,1
Member functions:
void input(): to input the size and the elements in the array
void Circular_Shift():to shift the elements of the array one place to the right.
First element comes to the second position, second element to the third and
ultimately the last element comes to the first position and so on.
void reverse(): reverse the array without using any temporary variable to
swap its elements.
89 | P a g e
void display(): to display the array in a circular fashion.
Specify the class CircularShift giving details of the constructor and member
functions void input(), void Circular_Shift(), void reverse(). void display()
define the main() function to create an object and call the functions
accordingly.
Algorithm
Step 1: Start
arr[i] = arr[i-1].
o arr[0] = temp.
90 | P a g e
Initialize two pointers: start = 0, end = n-1.
For i = 0 to n-1:
o For j = 0 to n-1:
Step 7: End
Program
91 | P a g e
92 | P a g e
Output
93 | P a g e
Question 25
94 | P a g e
A class matrix contains an integer array of order [m * n]. The maximum
value possible for both m and n is 10.
Design a class matrix to find the difference of the two matrices. The details
of the members of the class are given below:
Data members:
Member functions:
Specify the class Matrix, Define main (), giving the details of the above
member data and methods only.
Algorithm
1.Start
95 | P a g e
5.Display Both Matrices
6.Perform Subtraction
Inside fnSub():
8.End
Program
96 | P a g e
97 | P a g e
98 | P a g e
Output
99 | P a g e
Question 26
Display the original matrix, rearranged matrix and only the diagonal
elements of the
Algorithm
1.Start
3.Check validity
Else, continue.
100 | P a g e
Inner matrix = elements excluding boundary rows and columns.
Refill the sorted elements into their original inner matrix positions.
12.End
101 | P a g e
Program
102 | P a g e
103 | P a g e
Output
104 | P a g e
Question 27
105 | P a g e
Write a program to declare a square matrix A[][] of order MxM where ‘M’ is
the number of rows and thenumber of columns, such that M must be greater
than 2 and less than 10. Accept the value of M as user input.
Display an appropriate message for an invalid input. Allow the user to input
integers into this matrix.
original matrix
123
456
789
Rotated matrix
741
852
963
Find the sum of the elements of the four corners of the matrix.
Algorithm
1. Start
o Accept m.
3. Check validity
o Else, continue.
106 | P a g e
o For each row r from 0 to m-1:
o Initialize k = m - 1.
Decrease k by 1.
o Compute:
9. End
Program
107 | P a g e
Output
108 | P a g e
Question 28
109 | P a g e
Write a program to declare a matrix A[][] of order (MXN) where ‘M’ is the
number of rows and ‘N’ is the number of columns such M and N must be
greater than w and less than 20
Allow the user to input integers into this matrix. Perform the following tasks
on the matrix:
(b) Find the maximum and minimum value in the matrix and display them
along with their position.
(c) Sort the elements of the matrix in ascending order using any standard
sorting technique and rearrange them in the matrix.
Algorithm
1.Start
Check if (m > 2 && m < 20) and (n > 2 && n < 20).
o Else if current element > max[0], update max[0] and its position.
110 | P a g e
6.Flatten the matrix into a 1D array s
10.End
Program
111 | P a g e
112 | P a g e
Output
113 | P a g e
114 | P a g e