Beruflich Dokumente
Kultur Dokumente
QUESTION 1
Design a programme to accept a day number (between 1 and 366), year (in 4 digit) 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
conditions specified.
Test your programme for the following data and some random data.
Example 1:
InputDAY NUMBER
YEAR
DATE AFTER (N)
Output20 TH AUGUST 2008
DATE AFTER 17 DAYS
Example 2:
InputDAY NUMBER
YEAR
DATE AFTER (N)
Output25 TH DECEMBER 2008
DATE AFTER 45 DAYS
233
2008
17
360
2008
45
ALGORITHM
Algorithm for main() method
1.
2.
3.
4.
5.
6.
Start
Take day as input from console and store it in int variable d1
Take year as input from console and store it in int variable y1
Take N as input from console and store it in int variable n
Call the function Date as: obj.Date(d1,y1)
Check if n>=1 and n<=100, if yes then,
Assign d1=d1+n
Check if (y1%400==0) or (y%4==0) and (y%100!=0), if yes then,
Assign y1=y1+1
Assign d1=d1-366
Call the function Date as: obj.Date(d1,y1)
Else check if (y1%400!=0) and (y1%4!==0), if yes then,
Assign y1=y1+1
Assign d1=d1-365
Call the function Date as: obj.Date(d1,y1)
Else
Call the function Date as: obj.Date(d1,y1)
7. Else
Print "OUT OF RANGE"
8. End of main() method
Algorithm for function Date(int d, int y)
1. Initialise an int array as:
a[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
2. Check if (y1%400==0) or (y%4==0) and (y%100!=0), if yes then,
Assign a[1]=29
3. Initialise a String array as:
m[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST",
"SEPTEMBER","OCTEBER","NOVEMBER","DECEMBER"}
4. Initialise two int variables as i=0 and j=0
5. Initialise a String variable as month=""
6. Repeat steps 7 to 9 till d>a[i]
7. Assign d=da[i]
8. Increase value of i by 1
9. Assign j=i
10. Repeat step 11 for i=0 till i<11
11. Check if j==I, if yes then
Assign month=m[i]
12. Print d, month and y
13. End of function
SOURCE CODE
import java.io.*;
class QUESTION1
{
void DATE(int d,int y)
{
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if((y%400==0) || (Y%4==0) && (Y%100!=0))
a[1]=29;
String m[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE",
"JULY","AUGUST","SEPTEMBER","OCTEBER","NOVEMBER","DECEMBER"};
int i=0,j=0;
String month="";
while(d>a[i])
{
d=d-a[i];
i++;
j=i;
}
for(i=0;i<=11;i++)
{
if(j==i)
month=m[i];
}
System.out.println(d+"TH"+"\t"+month+" "+y);
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("DAY NUMBER\t");
int d1=Integer.parseInt(br.readLine());
System.out.print("YEAR \t\t");
int y1=Integer.parseInt(br.readLine());
System.out.print("DATE AFTER(N)\t");
int n=Integer.parseInt(br.readLine());
System.out.println("\n");
QUESTION1 obj=new QUESTION1();
obj.DATE(d1,y1);
if(n>=1 && n<=100)
{
d1=d1+n;
if(((y%400==0) || (Y%4==0) && (Y%100!=0)) && d1>366)
{
y1+=1;
d1-=366;
NAME
d
y
a[]
m[]
i
j
d1
y1
n
DATA TYPE
int
Int
int array
String array
int
int
int
Int
int
DESCRIPTION
As an argument for the function Date()
As an argument for the function Date()
An array to store the number of days of the months of a year
An array to store the name of the months of a year
To run a loop
To store the month number
To store the inputted day number
To store the inputted year
To store the inputted N
QUESTION 2
Write a programme 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 both m and n must be greater than 2 and less than 20. Allow the
user to input positive integer into these matrix. Perform the following tasks on the matrix:
(a) Sort the elements of the outer row and column in ascending order using any standard sorting
technique and arrange them in array.
(b) Calculate the sum of the outer row and column elements.
(c) Output the original matrix and the rearranged matrix.
Example:
Inputm=3
n=3
Original matrix:
Rearranged matrix:
1
8
6
7
2
3
4
5
9
1
9
8
3
2
7
4
5
6
Sum=43
Start
Initialise an int variable m with inputted number of rows
Initialise an int variable n with inputted number of columns
If m<=2 or m>=20 or n<=2 or n>=20 then print "OUT OF RANGE" and exit from program
Declare an int array A[][] with dimensions (m X n) as A[][]=new int[m][n]
Initialise two int variable i=0 and j=0
Repeat steps 7 to 10 till i<m
Repeat steps 8 and 9 till j<n
Take input from console and store it in A[i][j]
Increase value of j by 1
Increase value of I by 1
Assign j=0
Repeat steps 14 to 17 till i<m
Repeat step 15 and 16 till j<n
Print A[i][j]+"\t"
Increase value of j by 1
Increase value of I by 1
Assign j=0
Initialise an int variable with number of boundary elements of matrix A[][] as:
no=(m*2)+(n*2)4
Declare an int array e[] with dimension 'no' as e[]=new int[no]
Initialise two int variable k=0 and t=0
Assign i=0
Repeat steps24 to 26 from j=0 till j<n
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of j by 1
Assign i=m1
Repeat steps 29 to 31 from j=0 till j<n
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of j by 1
Assign j=0
Repeat steps 34 to from i=1 till i<m1
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of i by 1
Assign j=n1
Repeat steps 39 to 41 from i=1 till i<m1
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of i by 1
Repeat steps 43 to 46 from i=0 till i<k
Repeat steps 44 to 45 from j=0 till j<k
Check if e[j]>e[j+1], if yes then
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
Assign t=e[j]
Assign e[j]=e[j+1]
Assign e[j+1]=t
Increase value of j by 1
Increase value of I by 1
Initialise int variable sum=0
Repeat steps 49 and 50 from i=0 till i<k
Assign sum=sum + e[k]
Increase value of i by 1
Assign i=0, k=0
Repeat steps 53 and 54 from j=0 till j<n
Assign A[i][j]=e[k]
Increase value of k and j by 1
Assign j=n1
Repeat steps 57 and 58 from i=12 till i<m
Assign A[i][j]=e[k]
Increase value of k and i by 1
Assign i=m1
Repeat 61 to 63 from j=n2 till j=0
Assign A[i][j]=e[k]
Increase value of k by 1
Decrease value of j by 1
Assign j=0
Repeat steps 66 to 68 from i=m2 till i=0
Assign A[i][j]=e[k]
Increase value of k by 1
Decrease value of i by 1
Repeat steps 70 to 73 from i=0 till i<m
Repeat step 71 and 72 from j=0 till j<n
Print A[i][j]+ "\t"
Increase value of j by 1
Increase value of i by 1
End
10
10
11
11
12
12
NAME
m
n
i
j
A[][]
no
e[]
k
t
sum
DATA TYPE
int
int
int
int
int array
int
int array
int
int
int
DESCRIPTION
To store the number of rows
To store the number of columns
To run loops
To run loops
To store the elements inputted by user
To store the number of boundary elements
To store the boundary elements
To store the number of boundary elements
To use as a temporary variable during sorting
To store the sum of the boundary element
13
13
14
14
15
QUESTION 3
Read a single sentence which terminates with a full stop (.). The words are to be separated with a
single blank space and are in lower case. Arrange the words contained in the sentence according to
the length of the words in ascending order. If two words are of same length then the word occurring
first in the input sentence should come first. For both input and output, the sentence must begin in
upper case.
Test your programme for following data and some random data.
Input 1: The lines are printed in reverse order.
Output 1: In the are lines order printed reverse.
Input 2: Print the sentence in ascending order.
Output 2: In the print order sentence ascending.
Input 3: I love my country.
Output 3: I my love country.
15
11.
12.
13.
14.
15.
16.
17.
18.
Start
Take input from console
Convert it into lower case
Eliminate last character of the inputted string
Split the string using split() function and store it in a String array a[]
Initialise int l with the size of a[] as l=a.length
Declare String variable temp, int variable i and j
Repeat steps 9 to 12 from i=0 till i<l
Repeat steps 10 and 11 from j=0 till j<(li1)
If length of a[i] > length of a[i+1]
Assign temp=a[i]
Assign a[i]=a[i+1]
Assign a[i+1]=temp
Increase value of j
Increase value of i
Print first word of rearranged string with its first character changed to upper case
Repeat steps 15 and 16 from i=0 till i<(l1)
Print a[i]+" "
Increase value of i by 1
Print last word of rearranged string with full stop (.) at end as a[l1]+ "."
End
16
16
17
17
DATA TYPE
String
String array
int
int
int
int
DESCRIPTION
To store the inputted string
To store the words of the string
To store the number of words
Temporary variable to be used during sorting
To run a loop
To run a loop
18
18
19
19
20
QUESTION 4
A bank intends to design a program to display the denomination of an input amount, up to 5 digits.
The available denomination with the bank are of rupees 1000, 500, 100, 50, 20, 10, 5, 2 and 1.
Design a program to accept the amount from the user and display the break-up in descending order
of denomination, (i.e., preference should be given to the highest denomination available) along with
the total number of notes. [Note: Only the denomination used should be displayed]. Also print the
amount in words according to the digits.
Example 1:
INPUT- 14856
OUTPUTONE FOUR EIGHT FIVE SIX
DENOMINATORS:
1000X14=14000
500X1=500
100X3=300
50X1=50
5X1=5
1X1=1
TOTAL NUMBER OF NOTES: 21
Example 2:
INPUT- 6043
OUTPUTSIX ZERO FOUR THREE
DENOMINATORS:
1000X6=6000
20X2=40
2X1=2
1X1=1
TOTAL NUMBER OF NOTES: 10
Example 3:
INPUT- 235001
OUTPUTInput out of range.
20
21
21
22
22
23
23
NAME
rev
amount
amt
a
ch
den[]
i
tot
DATA TYPE
int
int
int
String
char
int array
int
int
DESCRIPTION
To store the number of notes of a particular denomination
To store the inputted amount
Dummy variable
To store the inputted amount
To store the extracted digits of the inputted amount
To store the available denominations
To run a loop
To store the total number of notes
24
24
25
25
26
QUESTION 5
A positive whole number 'n' having 'd' number of digits is squared and split into two pieces, a righthand 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 . . . . . . . . .
Example 1:
9
92=81, right-hand piece of 81=1 and left-hand piece of 81=8
Sum=1+8=9, i.e. equal to the number.
Example 2:
45
452=2025, right-hand piece of 2025=25 and left-hand piece of 2025=20
Sum=25+20=45, i.e. equal to the number.
Example 3:
297
2972=88209, right-hand piece of 88209=209 and left-hand piece of 88209=88
Sum=209+88=297, i.e. equal to the number.
Given two positive integers p and q, where p<q, write a programme to determine how many Kaprekar
numbers are there in the range between p and q (both inclusive) and output them.
The input contains two positive integers p and q. assume p<5000 and q<5000. You are to output the
number of Kaprekar numbers in the specified range along with their values in the format specified
below:
SAMPLE DATA:
InputP=1
Q=1000
OutputTHE KAPREKAR NUMBERS ARE:1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8
26
6.
7.
8.
9.
10.
11.
12.
13.
14.
Start
Take p as input from console and store bit in int variable p
Take q as input from console and store bit in int variable q
Initialise two int variable as i=0 and c=0
Check if p>5000 or q>5000 or p>q, if yes then
Print "OUT OF RANGE"
Else
Goto step 6
Repeat steps 7 to 12 for i=p till i=q
Assign int len with the length of i as len=(""+i).length()
Assign long sq=i*i
Assign long k=sq%(10^len)
Assign long s=sq/(10^len)
Assign long newn=k+s
Check if newn=I, if yes then
Print i
Increase value of c by 1
Print c
End
27
27
28
28
NAME
p
q
i
c
len
sq
r
l
newn
DATA TYPE
int
int
int
int
long
long
long
long
long
DESCRIPTION
To store the lower limit
To store the upper limit
To run a loop
Counter to count the number of Kaprekar numbers
To store the length of each number
To store the square of each number
To store the right hand piece of the square of the number
To store the left hand piece of the square of the number
To store the new number created by adding r and l
29
29
30
30
31
QUESTION 6
Input a paragraph containing 'n' number of sentences where (1=<n<4). The words are to be separated
with a single blank space and are in UPPERCASE. A sentence may be terminated either with a full stop
'. ', or a question mark '? ' only. Any other character may be ignored. Perform the following operations:
(a) Accept the number of sentences. If the number of sentences exceeds the limit, an appropriate
error message must be displayed.
(b) Find the number of words in the whole paragraph.
(c) Display the words in ascending order of their frequency. Words with same frequency may
appear in any order.
Example 1:
InputEnter number of sentences.
1
Enter sentences.
TO BE OR NOT TO BE.
OutputTotal number of words: 6
WORD
FREQUENCY
OR
1
NOT
1
TO
2
BE
2
Example 2:
InputEnter number of sentences.
3
Enter sentences.
THIS IS A STRING PROGRAM. IS THIS EASY? YES IT IS.
OutputTotal number of words: 11
WORD
FREQUENCY
A
1
STRING
1
PROGRAM
1
EASY
1
YES
1
IT
1
THIS
2
IS
3
Example 3:
InputEnter number of sentences.
5
OutputInvalid entry
31
32
32
33
33
34
34
NAME
n
s
i
j
t
temp
a[]
l
c[]
DATA TYPE
int
String
int
int
int
String
String array
int
int array
DESCRIPTION
To store the number of sentences
To store the sentences
To run a loop
To run a loop
Temporary variable used during sorting
Temporary variable used during sorting
Array to store the words
To store the number of words
Array to store the length of each word
35
35
36
36