0% found this document useful (0 votes)
53 views114 pages

Question 1

Uploaded by

tanishqk032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views114 pages

Question 1

Uploaded by

tanishqk032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 114

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

ENTER ELEMENTS FOR ROW 1: 1 1 3 7


ENTER ELEMENTS FOR ROW 2: 3 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT : FILLED MATRIX DECIMAL EQUIVALENT
1 1 3 7 607
3 1 0 6 1094
0 2 4 5 165

CODING

Solved the error by adding a semicolon.


3

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.

Example : Input : m=4,n=4

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

OUTPUT MATRIX : INNER MATRIX :


3 4 5 6
1 7 3 4 7 3
4 8 6 2 8 6
5 9 1 3

Example : Input : m=3,n=3

INPUT MATRIX :
1 2 3 Sum of left diagonal = 15
9

4 5 6 Sum of right diagonal = 15


7 8 9 Sum of outer elements = 79

OUTPUT MATRIX : INNER MATRIX :


1 2 3
4 5 6 5
7 8 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.

Example 1: INPUT m=4, n=4

INPUT MATRIX : ASCENDING ORDER:


6 4 2 9 0 2 4 5
7 0 8 10 6 7 7 8
12 5 98 23 9 9 10 12
30 7 12 9 12 23 30 98

DESCENDING ORDER: OUTPUT MATRIX:


98 30 23 12 6 4 2 9
12 10 9 9 7 0 8 10
8 7 7 6 12 5 98 23
5 4 2 0 30 7 12 9
15

Example 2: INPUT m=3, n=4

INPUT MATRIX : ASCENDING ORDER:


69 12 32 9 0 2 3 4
0 23 9 3 7 9 9 12
4 7 2 81 23 32 69 81

DESCENDING ORDER: OUTPUT MATRIX:


81 69 32 23 69 12 32 9
12 9 9 7 0 23 9 3
4 3 2 0 4 7 2 81
16
17

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

t int To convert DDA


into SDA

y int To convert SDA


into DDA
w int To convert DDA
into SDA
h int To convert SDA
into DDA
x int To store size of
SDA
a[ ] int To store elements
of matrix into
SDA
b[ ] int To store elements
of matrix into
SDA
A1[ ][ ] int To store the
sorted matrix
A2[ ][ ] int To store the
sorted matrix
20

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

MATRIX AFTER SORTING


ROWS:
-2 3 11
5 7 16
4 9 10
1 3 8
Example 2: INPUT: M=3, N=3
ENTER ELEMENTS OF THE MATRIX:
22 5 19
7 36 12
9 13 6
OUTPUT: ORIGINAL MATRIX:
22 5 19
7 36 12
9 13 6
MATRIX AFTER SORTING
ROWS:
5 19 22
7 12 36
6 9 13
23

Example 3: INPUT: M=11, N=5


OUTPUT: MATRIX SIZE OUT OF
RANGE
CODING

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:

Example 1: INPUT : m=5


n=100

OUTPUT : THE PRIME-ADAM INTEGERS ARE:


11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS:
3

Example 2: INPUT : m=100


n=200

OUTPUT : THE PRIME-ADAM INTEGERS ARE:


101 103 113

Example 3: INPUT : m=50


28

n=70

OUTPUT : THE PRIME-ADAM INTEGERS ARE:


NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS:
0

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

Write a program to accept a number from the


user and find the factorial of the number and
print it.
Test your program on the given data.

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

Write a program to accept a number from the


user and check whether it is an Armstrong
number or not. A number is said to be Armstrong
if the sum of its each digit and each digit is
raised to the power of total no. of digits in it is
equal to number itself.
{For example, 153=13 + 33 + 53 = 1 + 27 + 125 =
153}
Test your program on the given data.

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:

Write a program to accept a number from


user and check whether it is a Kaprekar
Number or not. A Kaprekar number is one
that square can be split into two equal
parts and sum these two parts equal to the
number itself. {For example, 9. 92 = 81 =
8+1 = 9}.
Test your program on the given data.
Example 1:
Input: 7
Output: 7 is not a Kaprekar Number.
Example 2:
Input: 45
Output: 45 is a Kaprekar Number.
Example 3:
Input: 7777
Output: 7777 is a Kaprekar Number.
41

Coding

Outputs
42

VARIABLE DESCRIPTION
DATA TYPE VARIABLE PURPOSE
NAME
int n To store input
number.
String s To convert the
squared number
43

into string for


easier
calculation.
int l To count the
number of
digits in the
squared
number.
int sum To get the sum
of the split
squared
number.

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

Step 6: Checking whether the sum of the two parts is


equal to the number itself or not.
Step 7: If Step 6 returns true printing (Entered number
is
a Kaprekar Number). Else, displaying
appropriate
message.
Step 8: Stop.

QUESTION 9:

Write a program to accept a number from


the user and check whether it is a
palindrome number or not. A number is said
to Palindrome if the number and the
reverse of the number both are equal.
{For example, 11 and 55}
Test your programs on the given data.
45

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:

Sample Input: CBBD


Output: BB

Example 2:

Sample Input: BABAD


Output: BAD ABA

Example 3:
50

Sample Input: aabcdcb


Output: bcdcb

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:

Write a program to accept a sentence in uppercase and


count the frequency of the palindrome words in the
sentence. Print the frequency and all the palindrome
words of the sentence. The sentence must be
terminated by ‘.’ Or ‘?’ or ‘!’ and if not display an
appropriate message.

Example 1:
Input Sentence: MOM AND DAD ARE AT HOME!
OUTPUT:
Frequency of palindrome words = 2
The palindrome words are: MOM DAD

Test your program on above and following data.

Example 2:

Input Sentence: SHE IS MY SISTER.


OUTPUT:
No palindrome words in the given sentence.

Example 3:

Input Sentence: HE IS A GREAT DANCER


OUTPUT:
INVALID INPUT!
56

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

ch, and l etc.


Step 3: Storing the inputted sentence in uppercase
from
user.
Step 4: Calculating length of the input sentence and
extracting its last character.
Step 5: Checking the termination condition
according
to the question and displaying appropriate
message if it is not fulfilled.
Step 6: Replacing the last character of the string with
a
blank and creating a variable to count
frequency
of palindrome words.
Step 7: Creating a while loop. Extracting each word of
the sentence one by one. And, calculating their
lengths.
Step 8: Creating a for loop to reverse each extracted
word and storing them in new string w. Closing
for loop.
Step 9: Checking if each word is palindrome or not, and
calculating their frequency and, storing them in
a
new string str.
60

Step 10: Closing while loop. Printing palindrome words


and their frequency if any, else displaying
appropriate message.
Step 11: Stop

QUESTION 12:

Write a program to accept a sentence in uppercase and


print the number of vowels and consonants in it. Also,
print the whole string in an alphabetical order. And, the
sentence must be terminated by a full stop, if not
display an appropriate message.
Test your program on the given data.
Example 1:
Input Sentence: I AM AN FAN OF ONE PIECE.
Output:
No. of Vowels = 10
No. of Consonants = 15
The alphabetically arranged sentence is:
AAACEEEFFIIMNNNOOP
Example 2:
Input Sentence: COMPUTER
Output:
INVALID INPUT!
Example 3:
Input Sentence: MY MOTHER IS BEST.
Output:
No. of Vowels = 4
61

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

Step 2: Declaring and initializing variables such as l, s


and ch etc.
Step 3: Storing the inputted sentence in uppercase
from
user.
Step 4: Calculating length of the input sentence and
extracting its last character.
Step 5: Checking the termination condition
according
to the question and displaying appropriate
message if it is not fulfilled.
Step 6: Replacing the last character of the string with
a
blank and creating variables for looping and
storing the number of vowels and consonants.
Step 7: Creating a for loop. Comparing each
character
of the string and printing the sentence in the
alphabetical order. Closing the for loop.
Step 8: Creating another for loop to count the
number
of vowels and consonants in the sentence.
Step 9: Calculating the number of vowels and
consonants and storing the in “vc” and “cc”
variables respectively.
Step 10: Printing the number of vowels and consonants.
66

Step 11: Stop.


QUESTION 13:
Write a Java program to check if two given strings are
anagrams of each other. Two strings are anagrams if one
string can be rearranged to form the other string. The
program should be case-insensitive and ignore spaces.
If they are not anagrams then display an appropriate
message.
Test your program on the given data.
Example 1:
Input Sentence: Silent
Listen
Output:
The two strings are the anagrams of each other.
Example 2:
Input Sentence: they see
The Eyes
Output:
The two strings are the anagrams of each other.
Example 3:
Input Sentence: sood
Mood
Output:
The two strings are not the anagrams of each
other.
67

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

DATA TYPE VARIABLE PURPOSE


NAME
String a To store input
string.
boolean same To check if the
character has its
duplicate or not.
int i For looping.
int j For Looping.

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

The given String: RecUrsIVE funCTioN


Total capital letters = 8
Total small letters = 9
2) Enter the String:

ComPUTer is mY favOURite subJECT


The given String: ComPUTer is mY favOURite subJECT
Total capital letters = 12
Total small letters = 16
3) Enter the String:

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

Variable Data type Purpose


name
Str String to store a string
cap int to count number of capital
letter
sm int
to count number of small letter
i int
for looping
ch char
to extract a character

Algorithm
Step 1: Start.
84

Step 2: Declare an initialize required variable such as Str, cap, sm.


Step 3: Input a sentence in Str.
Step 4: Start a for loop ,for i from 0 to the length of the string.
Step 5: Extracting a character from each word of a sentence.
Step 6: Now check whether it is a capital letter or a small letter.
Step 7: If it is capital then increase the value of cap by 1, otherwise increase
the value of sm by 1.
Step 8: Repeat the step 5, 6 and 7 until value of I reached to the value of
the length of the string.
Step 9: Display the original string and the total number of capital letter and
small letter.
Step 10: Stop.
85

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

Testing and Debugging


87

Solved by initializing variable ‘rev’

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

Computer is my favorite subject


The original sentence: Computer is my favorite subject
The new sentence: romputeC si ym eavouritf tubjecs
3) Enter the String

Winters are cold


The original sentence: Winters are cold
The new sentence: sinterW era dolc

CODING
93

COMPILING

Testing and Debugging


94

Solved by giving an argument(Str)

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

Accept two positive integer m and n, where m must be less


than n and the values of both m and n must be greater than 99
and less than 10000. Display all Fascinating number that are in
the range m and n(both inclusive) and output them along with
the frequency.
EXAMPLE:
1)
INPUT: m=100
n=500
THE FASCINATING NUMBER:
192 219 273 327
FREQUENCY OF FASCINATING NUMBERS IS:4
2)
INPUT: m=900
n=5000
THE FASCINATING NUMBER:
1902 1920 2019 2190 2703 2730 3027 3270
FREQUENCY OF FASCINATING NUMBERS IS:8
3)
INPUT: m=70
n=450
INVALID INPUT

CODING
10
0
10
1

COMPILING
10
2

OUTPUT
10
3
10
4

Variable Description
Variable name Data type Purpose

count int counting variable


num int to store a number
n1 int to store a number
n2 int to store a number
result String to store a string
len int to store the length of the
string
j int
for looping
c int
counting variable
ch char
for looping
ch1 char
for extracting a letter from
m int
word
n int
to store the minimum range
to store the maximum range

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

Here I have come to an end of the


computer project. I tried my best to
include all the points which were
necessary and are required for this
project. This project was very interesting
and I learned many things from it.

This project contains 20 programs which


were taken from books and some were
written my me. I do hope that my
11
2

project will be interesting and may be


knowledgeable.

Bibliography

I, Tanishq Kumar of class XII


have done this project with the
help of my parents, subject
teacher and my friends.

The websites which were used


by me.
1. Geeksforgeek.com
11
3

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

You might also like