Computer Programs Java
Computer Programs Java
Computer Programs Java
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:
Year =2018
OUTPUT:
Example 2:
INPUT:
Year =2018
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
EXAMPLE 1:
INPUT : N=3
Filled matrix 1 3 7
1 3 1
1 1 3
EXAMPLE 2:
INPUT: N=13
Write a program to print the numbers in spiral or circular form raking all natural
numbers from n2 to 1 (n>=1).
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
Write a program to input a natural number less than 10000 and then output it in words.
INPUT:29
INPUT:17
OUTPUT: seventeen
INPUT:119
INPUT:225
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.
Data members:-
Member method:-
Yarn()– default constructor to Initialize the data member with default value.
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:-
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.
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-
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-
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
Sub Class
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
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-
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:
SWITCH 82
ON 27
THE 33
FANS 39
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.
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.
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-
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.
Input: m=3
123
245
356
123
245
356
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:
OUTPUT:
Length: 4
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:
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
Sum = 25 + 20 = 45
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
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
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
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, 919, 929
Example 2:
INPUT:
m = 100
n = 5000
OUTPUT:
OUT OF RANGE.
QUESTION 24
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
123
654
789
INPUT: M = 4
1249
2583
1674
3765
OUTPUT:
ORIGINAL MATRIX
1249
2583
1674
3765
3121
7652
6784
5439
INPUT: M = 14
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:
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