Question 1
Question 1
QUESTION 1:
Write a program to declare a matrix A[ ][ ] of order(M x N)
where ‘M’ is the number of rows and ‘N’ is the number of
columns such that the value of ‘M’ must be greater than 0 and
less than 10 and the value of ‘N’ must be greater than 2 and
less than 6.
Allow user to input digits (0-7) only at each location, such that
each row represents an octal number.
Example 1 2 3 1
4 0 5
1 5 6
Decimal equivalent of 1st row = 153 i.e. 2x82+3x81+1x80=153
Decimal equivalent of 2nd row = 261 i.e. 4x82+0x81+5x80=261
Decimal equivalent of 3rd row = 110 i.e. 1x82+5x81+6x80=110
Perform the following tasks on the matrix:
(a) Display the original matrix.
(b) Calculate the decimal equivalent for each row and display
as per the format given below.
Test your program for the following data and some random
data.
Example 2:
INPUT: M=1
N=3
ENETR ELEMENTS FOR ROW 1: 1 4 4
OUTPUT : FILLED MATRIX DECIMAL EQUIVALENT
1 4 4 100
Example 3:
INPUT: M=3
N=4
2
CODING
OUTPUTS
4
5
6
7
ALGORITHM
Step 1: Start.
Step 2: Declare and initialize variables such as M,N, and double
dimensional array A[ ][ ], etc.
Step 3: Input number of rows in M and number of columns in N.
Step 4: Check if M<=0 OR M>10 OR N<=2 OR N>=6 then print
“OUT OF RANGE” and go to step 13.
Step 5: Start nested loops as, for I from 0 to M-1 and for j from
0
to N-1 and repeat steps 10 and 11.
Step 6: Input octal element/number in the matrix at A[i][j].
Step 7: Create a loop for I from 0 to M-1 and repeat steps 8 to
12.
Step 8: Initialize variable size=N-1 and 0 to variable decimal.
Step 9: Create a loop for j from 0 to N-1 and repeat step 10 and
11.
Step 10: Print octal number of the matrix stored at A[i][j];
Step 11: Evaluate decimal equivalent of the octal number of
the
matrix element stored at A[i][j] using the formula as:
decimal= decimal + A[i][j]*8 raised to the power
size-
1.
Step 12: After closing for j loop print the value stored in variable
decimal and shift the control to the new line, then
close
for I loop.
Step 13: Stop.
8
QUESTION 2:
Write a program to declare a matrix A[ ][ ] of order(mxn)
where ‘m’ is the number of rows and ‘n’ is number of columns
such that ‘m’ and ‘n’ must be less than or equal to 50.
Perform the following tasks on the matrix:
(a) Calculate the sum of right and left diagonals.
(b) Calculate sum of outer/boundary elements.
(c) Print the matrix row wise. Also, print only the inner
elements in the matrix form.
INPUT MATRIX :
3 4 5 6 Sum of left diagonal = 19
1 7 3 4 Sum of right diagonal = 22
4 8 6 2 Sum of outer elements = 47
5 9 1 3
INPUT MATRIX :
1 2 3 Sum of left diagonal = 15
9
CODING
10
OUPUTS
11
12
13
ALGORITHM
Step 1: Start
Step 2: Creating array and declaring some variables like i,j etc. for
further calculations.
Step 3: Inputting size of rows and columns of the matrix from the
user.
Step 4: Checking condition for the rows and columns, if satisfied
inputting matrix elements.
Step 5: Creating for loops to calculate sum of left diagonal.
Step 6: Closing for loops and creating another for loops to calculate
the sum of right diagonals.
Step 7: Closing for loops and creating another for loops to print only
inner elements of the matrix.
Step 8: Printing only inner elements of the matrix, closing for loops.
Step 9: Printing sum of right and left diagonals.
Step 10: Printing inner elements. Closing the main function.
Step 12: Stop.
14
QUESTION 3:
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
where m and n must be less than 20.
Perform the following the tasks on the matrix :
(a) Print the matrix row wise.
(b) Print the elements of the matrix in descending order using
bubble sort and in ascending order using selection sort.
OUTPUTS
18
Variable description
VARIABLE NAME DATA TYPE PURPOSE
A[ ][ ] int To store elements
of the matrix.
i int For looping
j int For looping
m int To store size of
the rows.
n int To store size of
the columns
small int To swap the
elements of the
matrix
temp int To swap the
elements of the
19
matrix
pos int To swap the
elements of the
matrix
ALGORITHM
Step 1: Start
Step 2: Creating essentials variables like A[ ][ ],m and n etc.
Step 3: Inputting values of rows and columns from the user.
Step 4: Declaring the size of double dimensional and single
dimensional array.
Step 5: Checking the conditions for rows and columns.
Step 6: If condition is satisfied creating for loops to input
elements of the matrix otherwise displaying appropriate
message.
Step 7: Ending for loops. And printing the original matrix.
Step 8: Converting matrix into one dimensional array.
Step 9: Starting for loops to sort the matrix in ascending order
using selection sort.
Step 10: Ending for loops and converting one dimensional array
into again matrix.
Step 11: Printing the sorted matrix.
Step 12: Repeating step 10.
Step 13: Strating for loops to sort the matrix into descending
order using bubble sort technique.
Step 14: Repeating step 12.
Step 15: Printing the sorted matrix.
Step 17: Stop.
21
QUESTION 4:
Write a program to declare a matrix A[ ][ ] of order (M
x N) where ‘M’ is the number of rows and ‘N’ is the
number of columns such as the value of both ‘M’ and
‘N’ must be greater than 2 and less than 10. Allow the
user to input integers into this matrix. Perform the
following tasks on the matrix:
( i ) Display the original matrix.
( ii ) Sort each row of the matrix in ascending order
using any standard sorting technique.
( iii ) Display the changed matrix after sorting each
row.
Test your program for the following data and some
random data:
Example 1:
INPUT : M=4
N=3
ENTER ELEMENTS OF MATRIX:
11 -2 3
5 16 7
9 10 4
3 1 8
OUTPUT: ORIGINAL MATRIX:
11 -2 3
5 16 7
9 10 4
22
3 1 8
OUTPUTS
24
25
26
ALGORITHM
Step 1: Start
Step 3: Creating essentials variable line AR[ ][ ],m and n etc.
Step 4: Inputting size of rows and columns by user.
Step 5: Checking conditions for the size of rows and columns.
Step 6: Inputting elements of the matrix if the conditions are
satisfied otherwise displaying proper message.
Step 7: Starting for loops to print The Original Matrix.
Step 8: Ending for loops. Again, starting 3 for loops to sort each
row of the matrix.
Step 9: Ending the 3 loops and starting 2 for loops to print the
row wise sorted matrix.
Step 10: Stop.
27
QUESTION 5:
A Prime-Adam integer is a positive integer( without leading
zeroes) which is prime as well as an Adam Number.
PRIME NUMBER: A number which has only 2 factors, i.e. 1
and the number itself.
Example : 2,3,5,7…………..etc.
ADAM NUMBER: The square of a number and square of its
reverse are revers to each other.
Example : If n=13 and reverse of n=31, then,
(13)2 = 169
(31)2 = 961, which is reverse of 169,thus 13, is an Adam
number.
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 frequency, in the format given below :
Test your program with the following data and some random
data:
n=70
CODING
29
OUTPUTS
30
ALGORITHM
31
Step 1: Start.
Step 3: Creating essential variables like sq,n,m etc.
Step 4: Inputting values for first and last limits from the users.
Step 5: Checking given condition for rows and columns and
displaying proper message if not satisfied.
Step 6: Creating for loop to check all the numbers in the limit.
Step 7: Checking if the number is prime or not.
Step 8: If the number is prim checking if it is Adam number or
not if not a prime number ignoring it.
Step 9: Counting the frequency of the Prime-Adam numbers
within in the limits.
Step 10: If frequency=0 displaying appropriate message if not
equal to 0 then also displaying appropriate message.
Step 11: Creating a function to reverse any number.
Step 12: Stop.
Question 6:
32
Example 1:
Input: 5
Output: The factorial of 5 is 120.
Example 2:
Input: 7
Output: The factorial of 7 is 5040.
Example 3:
Input: 10
Output: The factorial of 10 is 3628800.
CODING
33
OUTPUTS
34
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
double n To store input
number.
double f To store and
calculate the
factorial of the
number “n”.
double i For looping.
ALGORITHM
Step 1: Start.
Step 2: Declaring and initializing the variables such as n
and f.
Step 3: Getting input of a number from the user.
Step 4: Creating a for loop and declaring a variable “i”.
Step 5: Calculating the factorial of the inputted number
“n”.
Step 6: Printing the number along with its factorial.
Step 7: Stop.
QUESTION 7:
35
Example 1:
Input: 153
Output: It is an Armstrong Number.
Example 2:
Input: 1634
Output: It is an Armstrong Number.
Example 3:
Input: 300
Output: It is not an Armstrong Number.
36
CODING
OUTPUTS
37
38
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
int n To store input
number.
double f To calculate sum
of the digits of
the number.
double k To check
whether
inputted
number is an
Armstrong No.
or not.
double p To calculate
the no. of
digits in the
inputted
Number.
double r To extract each
digit of the
inputted
number.
String s To convert
inputted
number into
string.
39
ALGORITHM
Step 1: Start.
Step 2: Declaring and initializing variables such as n, f,
and r etc.
Step 3: Getting input from the user.
Step 4: Converting inputted number into a string and a
double data type.
Step 5: Getting length of the string number.
Step 6: Creating a do while to extract each digit and its
sum raised to the power of string(number)
length.
Closing do while loop.
Step 7: Checking whether the inputted number is an
Armstrong Number or not.
Step 8: Displaying appropriate message according to
Step
7.
Step 9: Stop.
40
QUESTION 8:
Coding
Outputs
42
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
int n To store input
number.
String s To convert the
squared number
43
ALGORITHM
Step 1: Start.
Step 2: Declaring and initializing variables such as n, s
etc.
Step 3: Converting the square of the number into
string.
And, calculating its length.
Step 4: Checking if the square of the number can be
spilt
into two parts.
Step 5: Calculating the sum of the two split parts if it
can
be split otherwise displaying appropriate
message.
44
QUESTION 9:
Example 1:
Input: 77
Output: 77 is a Palindrome Number.
Example 2:
Input: 89
Output: 89 is not a Palindrome Number.
Example 3:
Input: 101
Output: 101 is a Palindrome Number.
CODING
46
OUTPUS
47
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
int n To store input
number.
String s To convert
inputted number
into string.
int i For Looping.
int l To calculate
the no. of
digits in the
inputted
number.
String st To store the
reverse
number.
48
ALGORITHM
Step 1: Start.
Step 2: Declaring and initializing the variables such as
n,
s etc.
Step 3: Getting input from the user and converting
inputted number into string.
Step 4: Calculating the no. of digits in the number.
Step 5: Creating a for loop to reverse the number
string.
Step 6: Reversing the number and storing in a new
string.
Step 7: Comparing reversed and original string to check
if the number is a palindrome number or not.
Step 8: Printing appropriate message according to the
Step 7.
Step 9: Stop.
49
QUESTION 10:
Write a program to accept a single
string from the user and print the
longest Palindromic substring of the
inputted string.
Test your program on the following
data.
Example 1:
Example 2:
Example 3:
50
CODING
51
OUTPUTS
52
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
String s To store input
string.
int l To store length of
inputted string.
int i For Looping.
int j For Looping.
String st To store all
substrings as a
sentence.
String str To extract each
substring from st.
int f To calculate no. of
palindromic strings
to make an array.
int q To store length of
longest palindromic
substring.
int p To get length of
53
each substring
form st.
String t To swap the
palindromic
substrings.
int k To store substrings
in array.
String pa[ ] To store all
palindromic
substrings.
String w To reverse each
substring.
boolean swap To stop loop after
arranging all
palindromic
substrings in
ascending order.
ALGORITHM
54
Step 1: Start.
Step 2: Declaring and initializing variables such as s, j,
str etc.
Step 3: Storing all substrings into a new variable as a
single sentence.
Step 4: Creating while loop. Extracting each substring
from the sentence and checking if it is
palindrome or not.
Step 5: Calculating frequency of palindrome words for
array declaration and closing while loop.
Step 6: Declaring an array to store all palindromic
substrings. Creating a while loop.
Step 7: Transferring all palindromic substrings in the
array and closing while loop.
Step 8: Starting nested for loops and arranging the
array
in ascending order of the length of its
substrings.
Closing for loop.
Step 9: Getting length of longest palindromic substring
from the array. And, printing all palindromic
substring equal to its length.
Step 10: Stop.
55
QUESTION 11:
Example 1:
Input Sentence: MOM AND DAD ARE AT HOME!
OUTPUT:
Frequency of palindrome words = 2
The palindrome words are: MOM DAD
Example 2:
Example 3:
CODING
57
OUTPUTS
58
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
String st To store input
string.
String s To extract each
word of input
string.
String str To store
palindrome
words.
String w To store reversed
string.
int i For looping.
int l To store length
of input string.
int p To store length
of each
extracted
word.
int f To store
frequency of
palindrome
words.
ALGORITHIM
Step 1: Start.
Step 2: Declaring and initializing variables such as st,
59
QUESTION 12:
No. of Consonants = 14
The alphabetically arranged sentence is:
BEEHIMMORSSTTY
CODING
62
OUTPUTS
63
64
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
String s To store input
string.
char ch To extract last
character of
input string
char a For looping from
A to Z.
char chr To extract each
character of the
input string.
int i For looping.
int l To store length
of input string.
int cc To calculate
no. of
consonants.
int vc To calculate
no. of vowels.
ALGORITHIM
Step 1: Start.
65
CODING
68
OUTPUTS
69
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
String s To store input
string.
String st To store another
input string.
char a For looping from
A to Z.
char chr To extract each
character of the
input string
“st”.
int i For looping.
int l To store length
of input string.
char ch To extract each
character of the
input string “s”.
String w To store string
“s” in
alphabetically
order.
String w1 To store string
“st” in
alphabetically
order.
70
ALGORITHIM
Step 1: Start.
Step 2: Declaring and initializing variables such as s
and
st etc.
Step 3: Storing 2 inputted string from the user.
Step 4: Converting each string into uppercase.
Step 5: Removing blank spaces from both strings if any.
Step 6: Calculating lengths of both strings and also
creating a for loop.
Step 7: Creating another for loop inside previous one
and
extracting each character of both strings.
Step 8: Arranging both strings in alphabetical order and
storing in two different string variables. Closing
both loops.
Step 9: Comparing newly formed strings using
“compareTo ()” function.
Step 10: Displaying appropriate message if strings are
anagrams of each other or not.
Step 11: Stop.
71
QUESTION 14:
Write a program to accept a string from the user
and remove all the duplicate letters from the
string and print the original and modified string.
Test your program on the following data.
Example 1:
Input: Programming
Output: Original String = Programming
New String = Progamin
Example 2:
Input: Parrot
Output: Original String = Parrot
New String = Parot
Example 3:
Input: Srishti
Output: Original String = srishti
New String = sriht
72
CODING
OUTPUTS
73
VARIABLE DESCRIPTION
74
ALGORITHM
Step 1: Start.
Step 2: Declaring variable “a” to get input from the
user.
Step 3: Creating another variable “same” for future
calculations.
Step 4: Printing the original string and its first
character
in next line.
Step 5: Creating 2 nested for loops to find duplicate
characters throughout the string.
Step 6: Checking duplicate characters and printing the
modified string with no duplicate characters.
Closing the both for loops.
Step 7: Stop.
75
QUESTION 15:
Write a program to declare a matrix a[ ][ ] of order(mxn)
where ‘m’ is the number of rows and ‘n’ is number of
columns such that ‘m’ must be greater than or equal to
3 and less than or equal to 10 and ‘n’ must be less than
or equal to 10.Arrange the matrix using Bubble Sort
Technique in ascending order. Test your program on
following data.
Example 1:
Sample Input: m=3 n=3
Enter elements of the matrix: 2 1 0
4 2 9
21 12 14
Output: The Sorted Matrix: 0 1 2
2 4 9
12 14 21
Example 2:
Sample Input: m=4 n=3
Enter elements of the matrix: 98 1 45
1 86 523
58 0 2
84 78 98
Output: The Sorted Matrix: 0 1 1
2 45 58
78 84 86
98 98 523
Example 3:
Sample Input: m=5 n=11
Output: INVALID INPUT!
76
CODING
77
OUTPUTS
78
VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
int i For Looping.
int j For Looping.
int a[][] To store the
elements of a
matrix.
int tmp To swap the
elements of the
matrix.
int b[] To convert
matrix into
SDA.
int m To get input of
no. of rows.
int n To get input of
no. of columns.
int k To transfer
elements of
the matrix in
SDA.
79
ALGORITHM
Step 1: Start.
Step 2: Declaring and initializing variables like m, n, a[]
[]
etc.
Step 3: Getting input of rows and columns from the
user.
Step 4: Checking condition of rows and columns
according to the question and displaying
appropriate message if not true.
Step 5: Creating for loop and getting input of the
elements of the matrix. Closing the for loop.
Step 6: Creating for loop and transferring elements of
the
matrix in a SDA(b[ ]). Closing for loop.
Step 7: Creating 2 nested for loops and arranging
elements of SDA in ascending order.
Step 8: Swapping elements of the SDA in an ascending
order and closing both for loops.
Step 9: Again, transferring the sorted elements of the
SDA back into the matrix form.
Step 10: Displaying the sorted matrix using for loop.
Step 11: Stop.
80
Question no. 16
Write a program to accept a string and count the
number of capital letters and small letters in the string
and display it with the original string.
EXAMPLE:
1) RecUrsIVE funCTioN
ToDay is My BirTHdaY
The given String: ToDay is My BirTHdaY
Total capital letters = 7
Total small letters = 10
81
CODING
COMPILING
82
OUTPUT
Variable Description
83
Algorithm
Step 1: Start.
84
Question no. 17
Write a program to reverse a string and check whether
it is palindrome or not and display it with appropriate
message.
EXAMPLE:
1) Enter the String:
MALAYALAM
The original String: MALAYALAM
The reverse String: MALAYALAM
It is a Palindrome string
2) Enter the String:
LEVEL
The original String: LEVEL
The reverse String: LEVEL
It is a Palindrome string
3) Enter the String:
ANKIT
The original String: ANKIT
The reverse String: TIKNA
It is not a Palindrome string
CODING
86
COMPILING
OUTPUT
88
89
90
Variable Description
Variable name Data type Purpose
Str String to store a sentence
rev String to store a new string
i int for looping
ch char to extract a character
from a word
91
Algorithm
Step 1: Start.
Step 2: Declare an initialize required variable such as Str, rev, i.
Step 3: Input a sentence in Str.
Step 4: Start a for loop ,for i from length -1 of the string to 0.
Step 5: Extracting a character from each word of a sentence.
Step 6: Add the extracted word in the ‘rev’.
Step 7: Repeat the step 5, 6 and 7 until the value of i becomes 0.
Step 8: Display the original string and the reverse string.
Step 9: Check if the String is palindrome or not and display it with the
appropriate message.
Step 10: Stop.
92
Question no.18
Write a program to input a sentence and interchange
the first alphabet with the last alphabet for each word
in the sentence, with single letter the word remains
unchanged.
EXAMPLE:
1) Enter the String
It is a warm day
The original sentence: It is a warm day
The new sentence: tI si a marw yad
2) Enter the String
CODING
93
COMPILING
OUTPUT
95
96
97
Variable Description
Variable name Data type Purpose
String Str to store a sentence
String rev to store a new string
int i for looping
String word to store a word
char ch to extract a character
from a word
Algorithm
98
Step 1: Start.
Step 2: Declare an initialize required variable such as Str, rev.
Step 3: Input a sentence in Str.
Step 4: Create an object of String Tokenizer of String Str.
Step 5: Starting a while loop until it reached the value of number of words I
in sentence.
Step 6: Extracting each word from in ‘word’ from object st.
Step 7: Extracting first and last letter of each word and swap them.
Step 8: Store the new word in rev.
Step 9: Display the new sentence.
Step 10: Stop.
Question no.19
A Fascinating number is one which when multiplied by 2 and 3
then , after the results are concatenated with the original
number, the new number all the digits from 1 to 9 once. There
can be any of zeros and are to be ignored.
99
CODING
10
0
10
1
COMPILING
10
2
OUTPUT
10
3
10
4
Variable Description
Variable name Data type Purpose
Algorithm
Step 1: Start.
Step 2: Declare and initialize required variable such as m, n, n1, n2, len, ch, ch1, etc.
Step 3: Input start limit in variable m and end limit in variable n.
Step 4: Check if m>=n or m<99 or n<99 or m>10000 or n>10000 then print "INVALID
INPUT" and go to step 15 otherwise go to step 6 .
Step 5: Begin a loop using variable num from m to n and repeat step 5 to 11.
Step 6: Multiply num by 2 and store in n1 and multiply by 3 and store in n2.
10
5
Step 7: Concatenate num, n1 and n2 and store in variable result and find the length of
concatenated number.
Step 8: Begin a loop using variable ch from '1' to '9' and repeat step 10 to 12.
Step 9: Begin another loop using variable j from 0 to length and repeat step 9.
Step 10: Extract a character from concatenated number and check if ch and ch1 are same
then add 1 to c.
Step 11: Check if c not equal to 1 then terminate the outer loop controlled loop by
variable ch.
Step 12: Check if value of count is greater than '9' then add 1 variable count and print
num as fascinating number.
Step 13: Check if value of count is greater than 0 then print count as the frequency of
fascinating number otherwise check if count equal to 0 then print NIL
and count as the frequency of fascinating number as 0.
Step 14: Stop.
Question no.20
Write a program to input a number and check if a given
number is a magic number or not and display it with a
appropriate message. (A magic number is a number in
which the eventual sum of digits of the number is equal
to 1).
EXAMPLE:
1)
Enter the Number:
172
172 is a magic number
10
6
2)
Enter the Number:
163
163 is a magic number
3)
Enter the Number:
495
495 is not a magic number
CODING
10
7
COMPILING
10
8
OUTPUT
10
9
Variable Description
Variable name Data type Purpose
11
0
num int to store a number
q int to store a number
sum int to store the sum
s int to store a number
Algorithm
Step 1: Start.
Step 2: Declare an initialize required variable such as num, sum, s, q.
Step 3: Input a number in num.
Step 4: Begin a while loop until s is greater than 9 and repeat step 5.
Step 5: Begin another loop until q is greater than 0 and repeat step 6
Step 6: Store the sum by extracting the last digit of the number q and divide
the number by 10 to remove the last digit.
Step 7: Check if ‘s’ is equal to 1 and display with the appropriate message.
Step 8: Stop.
11
1
Conclusion
Bibliography
2. www.knowledgeboat.com
3. www.doubtnut.com
4. ww.playgroundai.com
Glossary
int – integer
char – character
util – utility package
args – argument
ld – left diagonal
rd – right diagonal
os – sum of outer elements
sqrt – square root
pow – raise to the power
rev – reverse
on – Original number
11
4