You are on page 1of 26

QUESTION 1

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 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
for the following data and some random data.

Example 1:

INPUT:

Day number = 255

Year =2018

Date after m days = 22

OUTPUT:

Date = 12th September,2018

Date after 22 days = 4th October,2018

Example 2:

INPUT:

Day number = 500

Year =2018

Date after m days = 33

OUTPUT:

INVALID INPUT
QUESTION 2

Write a program to declare a single dimensional array a[] and a square matrix b[][] of
size N, where N>2 and N<10. Allow the user to input positive integers into the single
dimensional array. Perform the following tasks on the matrix:

Sort the element of the single dimensional array in ascending order using any standard
sorting technique and display the sorted elements.

b) Fill the square matrix b[][]in the following format. If the array a[]={5,2,8,1} then,
after sorting a[]={1,2,5,8} Then the matrix b[][] would fill as bellow:

1 2 5 8

1 2 5 1

1 2 1 2

1 1 2 5

c) Display the filled matrix in the above format.

EXAMPLE 1:

INPUT : N=3

ENTER THE ELEMENTS OF SINGLE DIMENSIONAL ARRAY : 3 1 7

OUTPUT : Sorted array : 1 3 7

Filled matrix 1 3 7

1 3 1

1 1 3

EXAMPLE 2:

INPUT: N=13

OUTPUT: MATRIX SIZE OUT OF RANGE


QUESTION 3

Write a program to print the numbers in spiral or circular form raking all natural
numbers from n2 to 1 (n>=1).

Let Input n = 4, then n2 = 16, the output matrix is:

16 15 14 13

5 4 3 12

6 1 2 11

7 8 9 10
QUESTION 4

A natural number N is said to be a SMITH NUMBER, if sum of all the digits of N and
sum of all the digits of prime factors of N are same.

Example :

INPUT

N=666

OUTPUT

666 is a smith number


QUESTION 5

Write a program to input a natural number less than 10000 and then output it in words.

Test your program for the following set of data:

INPUT:29

OUTPUT: twenty nine

INPUT:17

OUTPUT: seventeen

INPUT:119

OUTPUT: one hundred nineteen

INPUT:225

OUTPUT: two hundred twenty-five


QUESTION 6

Define a class Yarn that accepts a string and perform the following:-

Replace each vowel with next alphabet and replace each consonant with previous
alphabet.

Class name:- Yarn

Data members:-

String S — to store the sentence.

Member method:-

Yarn()– default constructor to Initialize the data member with default value.

Void accept()– to input a sentence and store it in S.

String fnmarker()– to give the modified string to a given string by applying the
criteria and returning the formalized string.

Void show– to display both the string, original and new with message.

Also write the main function to create the object of Yarn class and call the respective
function.

Example:-

Input— ALL ZEBRAS ARE STRIPPED.

Output— BKK YFAOBR BOF RSQJOOFC.


QUESTION 7

Design a class Twist that accepts a number n,that has even no of digits. Create a new
no. n2 from n such that each pair of digits from left to right are reversed. In case n has
odd no. of digits,then n2=n.The details of the class Twist is given below-

Class name-Twist
Data members- n-to store the no. given by user
n2-to store the reversed no.
Member methods-Twist()-default constructor
void input()-to accept a no. from user
void modify()-to modify n to create n2
int countofdigit(int m)-to count the no. of digit using recursive technique
void show()-to display data member with appropriate message.

Specify the class Twist giving the details of the constructor and the member
methods.Define the main function to create an object of Twist class and call the
member functions accordingly.

Example 1:Input n=427158


Output n2=241785

Example 2:Input n=123


Output n2=123
QUESTION 8

Dequeue is an entry which can hold 200 elements. This data structure allows insertion
and deletion of elements from both the ends,i.e.,front and rear.The details of the class
Dequeue are as follows-

Class name: Dequeue


Data members: dque[]-integer array which can hold maximum 200 elements

cap-integer to store the maximum limit of dque


front-integer to point to the index of the front end of dque
rear-integer to point to the last index of the dequeue
Member methods:Dequeue(int,int): constructor to initialise front=0,rear=-1
void pushatrear(int num): add the integer num to the rear of dequeue if possible
otherwise print a message ”Overflow from rear”
void pushatfront(int num): add num to the front of dequeue if possible otherwise print
the message “Overflow from front”
int popfront():return an integer from front of dequeue if possible. Otherwise print a
message “Dequeue is empty” and return -999
int poprear():return the value from rear of dequeue if possible otherwise print the
message “Dequeue is empty”.-999 will be returned if the dequeue is empty.
void dequeuedisplay(): for displaying the elements present in the dequeue.
Specify the class dequeue by designing constructor and the member functions. Define
a main function to create an object and call the methods respectively.
QUESTION 9

A transpose of an array is obtained by interchanging the elemnts of rows and columns.


A class Transarray contain a 2D array of order m*n. The maximum value possible for
both m and n is 20. Design a class Transarray to find transpose of a matrix. The details
of members of the class are given below-
Class name: Transarray
Instance variables: arr[][]: integer to store the matrix elements
m,n: integers to store the numbers of rows and columns present in the arr[][]
Member methods: Transarray(): default constructor to initialise the data members with
default values
Transarray(int num,int no): parameterised constructor to initialise m=num and n=no
void fillarray(): to enter the element in matrix arr[][] from user

void transpose(Transarray A): to find and store the transpose of matrix arr and from
object A
void display(): display the transposed array in matrix format.

Specify the class Transarray with the details given. Define the main function to create
object and call the member functions accordingly.
QUESTION 10

A class Matrix contains a 2D integer array of order m*n. The maximum value for m
and n is 25. Design a class matrix to find the difference of 2 matrices. The details is
given below-

Class name: Matrix


Data member: arr[][]: store the matrix elements
m.n: integer to store the number of rows and columns
Member methods: Matrix(int num,int no): parameterised constructor to store m=num
and n=no
void fillarray(): to enter the matrix element
Matrix subMat(Matrix A): subtract the current object from the matrix of parameterised
object and return the resulting object
void display():display the matrix in matrix format.

Specify the class Matrix giving details of constructors and member methods. Define
the main function to create object and call the member methods respectively.
QUESTION 11

A superclass Product has been defined to store the details of a product sold by
wholeseller to retailer. Define a subclass Sales to compute the total amount paid by
retailer with/without fine along with the service tax. Some of the members of both the
classes are as follows-

Base Class

Class name: Product

Data member: name: to store the name of the product


code: integer to store product code
amt: to store the sale amount of product

Member method: Product(String n,int c,double p): initialise name=n,code=c,amount=p


void show(): display the details of data members

Sub Class

Class name: Sales


Data member: day: store the number of days taken to pay the sale amt
tax: to store the total amount of tax(service)
totamt: to store the total amount in decimal
Member methods: Sales(…): parameterised constructor to initialise the data members
of both classes
void compute(): calculate the service tax at the rate of 12.5% of actual sale
amt,calculate the fine @2.5% of the actual sale amount paid by retailer to wholeseller.
If days exceed 30days,calculate the total amount paid by the retailer-actual sale
amount+service tax+fine
void show(): display the data members of superclass and totamt from subclass.

Using the concept of inheritance, design both classes. Write the main function to
create object of the class and invoke the necessary functions.
QUESTION 12

The following are the ways of finding n raised to the power m.


Example- Let m=2
and n=0
mn=20=1
A class Poweris is declared with the following details.
Class name: Poweris

Data members: base: integer to store number in base


expm: integer to store the power of base
p(double type): to store base raised to the power exp.
Member methods: poweris(): the default constructor to initialise all data members with
default values
int power(int a,int b): to calculate and return a raised to b using recursive technique
void findres(): to accept base and expm and store base raised to power (int)p
void print(): print the value of p
With suitable documentation,specify the class Poweris showing all the constructor and
method details. Also write main function to create the object and call the member
methods respectively.
QUESTION 13

A class RevStr defined a recursive function to reverse a string and check whether it is
palindrome or not. The details of the class are given below-

Class name: RevStr


Data member: str: to store a string
rev: to store the reverse of the string
Member methods:

void getstr(): to accept a string and store in str

void recReverse(int): to reverse the string using recursive technique

void check(): to display the original string and the reversed string and check whether
the string is palindrome or not

Specify the class with all the member methods. Also write the main function to create
the objects of the class and call the member methods respectively.
QUESTION 14

The encryption of alphabets are to be done as A=1, B=2, C=3,…Z=26. The strength of
the word is found by adding the encrypted values of each alphabets. For example let
the word be “light”, then the strength of the word is 12+9+7+8+20=56. Write a
program to accept a sentence in uppercase which may be terminated by “,”,”.”,”!”
signs. The words re separated by only one blank space. Decode the word according to
their strength and arrange the ascending order of their strength along with the sorted
sentences. Test your program with the following sample data:

Input: SWITCH ON THE FANS

Output: word strength

SWITCH 82

ON 27

THE 33

FANS 39

Sorted sentence: ON THE FANS SWITCH


QUESTION 15

Given 2 positive numbers m and n such that m is between 100 to 10000 and n is less
that 100.

Find the smallest integer that is greater than m and whose digits add upto n.

If m= 100 and n=1

The smallest integer than 100 whose digits add upto 11 is 119. write a program to
accept the numbers m and n from the user and print the required number where sum of
all digits is equal to n. Also print the total number of digits present in required number.
The program should check for validity of inputs and display an appropriate message
for incorrect data. Test your program with the following sample data.

Input: m=1800 n=25

The required number is 1698

Total number of digit= 4


QUESTION 16

Write a program to declare a square matrix a[][] of order m×m, where m is eual to the
number of rows and columns. M should be greater than 2 and less than 10. Accept the
value of m as input. Display an appropriate message for an invalid input. Allow the
user to enter matrix element and perform the following tasks-

i) Display the original matrix in matrix format

ii) check if the given matrix is symmetric or not.

A square matrix is said to be symmetric if element of its i-th row and i-th column is
equal to the element of j-th row and j-th column.

iii) find the sum of element of left and right diagonals of the matrix and display them.

Test your program using the following data:

Input: m=3

123

245

356

Output: original matrix:

123

245

356

The given matrix is symmetric

Sum of left diagonal=11

Sum of right diagonal=10


QUESTION 17

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:

Obtain the length of the sentence (measured in words)

Arrange the sentence in alphabetical order of the words.

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

Example 1: INPUT: BE GOOD TO OTHERS.

OUTPUT:

Length: 4

Rearranged Sentence: BE GOOD OTHERS TO


QUESTION 18

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 for A, , z are 65, 32, 122 respectively. Concatenate them to get 6532122, 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


QUESTION 19

A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two
pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining
‘d’ or ‘d-1’ digits.

If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number.
The first few Kaprekar numbers are: 9, 45, 297 …….. Write a program to enter a
number from user. Check the number is kaprekar number or not. Test your program
with the following sample data:

Example 1: Input: 45

452 = 2025

Right-hand piece of 2025 = 25

Left hand piece of 2025 = 20

Sum = 25 + 20 = 45

Output: 45 is a Kaprekar number.


QUESTION 20

A positive natural number, (for e.g. 27), can be represented as follows:


2+3+4+5+6+7
8+9+10
13+14
where every row represents a combination of consecutive natural numbers, which add
up to 27.

Write a program which inputs a positive natural number N and prints the possible
consecutive number combinations, which when added give N.

Test your program for the following data and some random data.
Example 1: INPUT: N = 9
OUTPUT:
4+5
2 + 3+ 4

Example 2: INPUT: N = 21
OUTPUT:
10+ 11
1+ 2+ 3+ 4+ 5+ 6
6+ 7+ 8
QUESTION 21

Consider the sequence of natural numbers.


1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
………………………………….

Removing every second number produces the sequences


1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 ………………………….

Removing every third number produces the sequences


1, 3, 7, 9, 13, 15, 19, 21, 25 ………………………….

This process continues indefinitely by removing the fourth, fifth…and so on, till after
a fixed number of steps, certain natural numbers remain indefinitely. These are known
as Lucky Numbers.

Write a program to generate and print lucky numbers less than a given number
n,where n<=50. Test your program using following sample data.

INPUT : N = 25
OUTPUT : The Lucky numbers less than 25 are: 1,3,7,13,19
QUESTION 22

A class sum_series is declared to find the sum of following series:


S=x2/11+x4/12+x6/13+x8/14+…….
Some member methods of the class are given below:
Class name: sum_series

Data members: x: integer to store a number


n: to store the number of terms

Member methods: sum_series(): default constructor


void readlimit(): to accept x and n from user
int getpower(int m,int p): to find and return mp
void sumseries(): to calculate sum of series by invoking function getpower.

Specify the class sum_series with all details. Also write the main function to create the
object of the class sum_series and call the respective functions.
QUESTION 23

A prime palindrome integer is a positive integer (without leading zeroes) which is


prime as well as a palindrome. Given two positive 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:

THE PRIME PALINDROME INTEGERS ARE:

101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 919, 929

FREQUENCY OF PRIME PALINDROME INTEGERS: 15

Example 2:

INPUT:

m = 100

n = 5000

OUTPUT:

OUT OF RANGE.
QUESTION 24

Write a program to declare a square matrix A[][] of order MxM where


‘M’ is the number of rows and the number of columns, such that M is greater than 2
and less than 10. Accept the value of M as user input. Display an appropriate message
for invalid input. Allow the user to input integers into this matrix. Perform the
following tasks:
(a) Display the original matrix.
(b) Rotate the matrix 90 degree clockwise as shown below:
Original matrix Rotated matrix

123 741

456 852

789 963
(c) Find the sum of the elements of the four corner elements.
Test your program for the following data and some random data.
Example 1:

INPUT: M = 3

349

258

167

OUTPUT:

ORIGINAL MATRIX

349

258

167

MATRIX AFTER ROTATION

123

654
789

Sum of corner elements = 20


Example 2

INPUT: M = 4

1249

2583

1674

3765

OUTPUT:

ORIGINAL MATRIX

1249

2583

1674

3765

MATRIX AFTER ROTATION

3121

7652

6784

5439

Sum of corner elements = 18


Example 3

INPUT: M = 14

OUTPUT: SIZE IS OUT OF RANGE


QUESTION 25

Write a program to declare a square matrix M [ ] [ ] of order ‘N’ where ‘N’ must be
greater than 3 and less than 10. Allow the user to accept three different characters
from the keyboard and fill the array according to the instruction given below:

(i) Fill the four corners of the square matrix by character 1.


(ii) Fill the boundary elements of the matrix (except the four corners) by character 2.
(iii) Fill the non-boundary elements of the matrix by character 3.

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

Example 1:
INPUT: N = 4
FIRST CHARACTER: Q
SECOND CHARACTER: $
THIRD CHARACTER: ?
OUTPUT:
Q$$Q
$??$
$??$
Q$$Q

Example 2: INPUT: N = 13
OUTPUT: SIZE OUT OF RANGE

You might also like