Вы находитесь на странице: 1из 102

COMPUTER SCIENCE ASSIGNMENT

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

6TH SEPTEMBER 2008

360
2008
45

8TH FEBRUARY 2009

COMPUTER SCIENCE ASSIGNMENT


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","AU
GUST", "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

COMPUTER SCIENCE ASSIGNMENT


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;

COMPUTER SCIENCE ASSIGNMENT


System.out.print("DATE AFTER "+n+" DAYS ");
obj.DATE(d1,y1);
}
else if(y1%4!=0 && Y1%400!=0 && d1>365)
{
y1+=1;
d1-=365;
System.out.print("DATE AFTER "+n+" DAYS ");
obj.DATE(d1,y1);
}
else
{
System.out.print("DATE AFTER "+n+" DAYS ");
obj.DATE(d1,y1);
}
}
else
System.out.println("OUT OF RANGE");
}
}

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION

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 nos. 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

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

COMPUTER SCIENCE ASSIGNMENT


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

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
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.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
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

COMPUTER SCIENCE ASSIGNMENT


44. Check if e[j]>e[j+1], if yes then
Assign t=e[j]
Assign e[j]=e[j+1]
Assign e[j+1]=t
45. Increase value of j by 1
46. Increase value of I by 1
47. Initialise int variable sum=0
48. Repeat steps 49 and 50 from i=0 till i<k
49. Assign sum=sum + e[k]
50. Increase value of i by 1
51. Assign i=0, k=0
52. Repeat steps 53 and 54 from j=0 till j<n
53. Assign A[i][j]=e[k]
54. Increase value of k and j by 1
55. Assign j=n1
56. Repeat steps 57 and 58 from i=12 till i<m
57. Assign A[i][j]=e[k]
58. Increase value of k and i by 1
59. Assign i=m1
60. Repeat 61 to 63 from j=n2 till j=0
61. Assign A[i][j]=e[k]
62. Increase value of k by 1
63. Decrease value of j by 1
64. Assign j=0
65. Repeat steps 66 to 68 from i=m2 till i=0
66. Assign A[i][j]=e[k]
67. Increase value of k by 1
68. Decrease value of i by 1
69. Repeat steps 70 to 73 from i=0 till i<m
70. Repeat step 71 and 72 from j=0 till j<n
71. Print A[i][j]+ "\t"
72. Increase value of j by 1
73. Increase value of i by 1
74. End

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION2
{
public static void main(String args[])throws IOException
{
int m, n, i, j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter m: ");
m=Integer.parseInt(br.readLine());
System.out.print("Enter n: ");
n=Integer.parseInt(br.readLine());
if(m<=2 || m>=20 || n<=2 || n>=20)
{
System.out.println("OUT OF RANGE");
System.exit(0);
}
int A[][]=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print("Enter element ["+i+"]["+j+"]:");
A[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("\n");
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int no=(m*2)+(n*2)-4;
int e[]=new int[no],k=0,t=0;
i=0;
for(j=0;j<n;j++)
{
e[k]=A[i][j];
k++;
}
i=m-1;

10

10

COMPUTER SCIENCE ASSIGNMENT


for(j=0;j<n;j++)
{
e[k]=A[i][j];
k++;
}
j=0;
for(i=1;i<m-1;i++)
{
e[k]=A[i][j];
k++;
}
j=n-1;
for(i=1;i<m-1;i++)
{
e[k]=A[i][j];
k++;
}
for(i=0;i<k;i++)
{
for(j=0;j<k-i-1;j++)
{
if(e[j]>e[j+1])
{
t=e[j];
e[j]=e[j+1];
e[j+1]=t;
}
}
}
int sum=0;
for(i=0;i<k;i++)
{
sum+=e[i];
}
i=0;
k=0;
for(j=0;j<n;j++)
{
A[i][j]=e[k];
k++;
}
j=n-1;
for(i=1;i<m;i++)
{
A[i][j]=e[k];
k++;

11

11

COMPUTER SCIENCE ASSIGNMENT


}
i=m-1;
for(j=n-2;j>=0;j--)
{
A[i][j]=e[k];
k++;
}
j=0;
for(i=m-2;i>0;i--)
{
A[i][j]=e[k];
k++;
}
System.out.println("\n");
System.out.println("REARRANGED MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("SUM= "+sum);
}
}

12

12

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

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

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

14

14

COMPUTER SCIENCE ASSIGNMENT


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

15

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

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

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION3
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence:");
String s1=(br.readLine()).toLowerCase();
s1=s1.substring(0,s1.length()-1);
String a[]=s1.split(" ");
int l=a.length;
String temp;
int i,j;
for(i=0;i<l;i++)
{
for(j=0;j<(l-i-1);j++)
{
if(a[j].length()>a[j+1].length())
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print((Character.toUpperCase(a[0].charAt(0)))+a[0].substring(1)+" ");
for(i=1;i<l-1;i++)
System.out.print(a[i]+" ");
System.out.println(a[l-1]+".");
}
}

17

17

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE
NAME
s1
a[]
l
temp
i
j

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

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

19

19

COMPUTER SCIENCE ASSIGNMENT


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

20

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take input from console and store it in int variable amount
3. Check if amount>99999, if yes then
Print "INVALID AMOUNT"
Exit from program
4. Initialise int amt=amount, rev=0
5. Repeat steps 6 to 8 from i=0 till i<length of amt
6. Initialise char ch with the character at I in amt
7. Check ch with digits 0 to 9 and print the corresponding digit in words
8. Increase value of i by 1
9. Initialise an int array den[] with the available denominations as:
den[]={1000, 500, 100, 50, 20, 10, 5, 2, 1}
10. Initialise two int variable i=0 and tot=0
11. Repeat steps 12 to 15 till amount not equals 0
12. Assign rev=amount/den[i]
13. If rev not equals 0, then
Print (den[i]+ "X" +rev + "=" rev*den[i])
Assign tot=tot+rev
14. Assign amount=amount%den[i]
15. Increase value of i by 1
16. Print ("Total number of notes: "+tot)
17. End

21

21

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QESTION4
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int rev=0, amount, amt;
System.out.print("Enter the amount:");
String a=br.readLine();
amount=Integer.parseInt(a);
if(amount>99999)
{
System.out.println("INVALID AMOUNT");
System.exit(0);
}
amt=amount;
for(int i=0;i<a.length();i++)
{
char ch=a.charAt(i);
switch(ch)
{
case '0':
System.out.print("ZERO");
break;
case '1':
System.out.print("ONE");
break;
case '2':
System.out.print("TWO");
break;
case '3':
System.out.print("THREE");
break;
case '4':
System.out.print("FOUR");
break;
case '5':
System.out.print("FIVE");
break;
case '6':
System.out.print("SIX");
break;
case '7':
System.out.print("SEVEN");
break;

22

22

COMPUTER SCIENCE ASSIGNMENT


case '8':
System.out.print("EIGHT");
break;
case '9':
System.out.print("NINE");
}
System.out.print(" ");
}
int den[]={1000,500,100,50,20,10,5,2,1};
int i=0,tot=0;
System.out.println();
System.out.println("\nDENOMINATION:\n");
while(amount!=0)
{
rev=amount/den[i];
if(rev!=0)
{
System.out.print("\t\t"+den[i]+"\tX\t"+rev+"\t=\t"+rev*den[i]);
System.out.println();
tot+=rev;
}
amount=amount%den[i];
i++;
}
System.out.println("\t\tTOTAL\t\t\t=\t"+a);
System.out.println();
System.out.println("TOTAL NUMBER OF NOTES\t=\t"+tot);
}
}

23

23

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

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

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

25

25

COMPUTER SCIENCE ASSIGNMENT


QUESTION 5
A positive whole number 'n' having '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 . . . . . . . . .
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

26

COMPUTER SCIENCE ASSIGNMENT


Algorithm
1.
2.
3.
4.
5.

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

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION5
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("p= ");
int p=Integer.parseInt(br.readLine());
System.out.print("q= ");
int q=Integer.parseInt(br.readLine());
int i=0,c=0;
if(p>5000 || q>5000 || p>q)
System.out.println("OUT OF RANGE");
else
{
System.out.println("THE KAPREKAR NUMBERS ARE:-");
for(i=p;i<=q;i++)
{
int len=(""+i).length();
long sq=i*i;
long r=sq%(long)(Math.pow(10,len));
long l=sq/(long)(Math.pow(10,len));
long newn=(r+l);
if (newn==i)
{
System.out.print(i+",");
c++;
}
}
System.out.println("\n"+"FREQUENCY OF KAPREKAR NUMBBERS IS: "+c);
}
}
}

28

28

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

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

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

30

30

COMPUTER SCIENCE ASSIGNMENT


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.
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

31

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take the number of sentences as input from console and store it in int variable n
3. Check if n<1 or n>=4, if yes then
Print "Invalid entry"
Exit from program
4. Take the sentences as input from console and store it in a String variable s
5. Convert s to UPPERCASE
6. Initialize int i=0, j=0, t=0
7. Initialize String temp=""
8. Replace full stop (.) and question mark (?) in the sentences with a blank space (" ")
9. Split the words using split() function and store them in a String array a[]
10. Find the number of words and store it in int l as :
l=a.length
11. Declare an int array c[] of size l
12. Print ("Total number of words: "+l)
13. Repeat steps 14 to 18 from i=0 till i<l
14. Assign c[i]=1
15. Repeat steps 16 and 17 from j=i+1 till j<l
16. Check if a[i] equals a[j], if yes then,
Increase value of c[i] by 1
Assign a[j]= ""
17. Increase value of j
18. Increase value of i
19. Repeat steps 20 to 24 from i=0 till i<l
20. Check if a[i] not equals "" , then go to 21 else go to 24
21. Repeat steps22 and 23 from j=0 till j<l
22. Check if c[i]<c[j], if yes then ,
Assign t=c[i]
Assign c[i]=c[j]
Assign c[j]=t
Assign temp=a[i]
Assign a[i]=a[j]
Assign a[j]=temp
23. Increase value of j by 1
24. Increase value of I by 1
25. Repeat steps 26 and 27 from i=0 till i<l
26. Check if a[i] not equals "", then,
Print (a[i]+ "\t\t"+c[i])
27. Increase value of I by 1
28. End

32

32

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION6
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of sentences.");
int n=Integer.parseInt(br.readLine());
if(n<1 || n>=4)
{
System.out.println("Invalid entry");
System.exit(0);
}
else
{
System.out.println("Enter the sentence(s).");
String s=(br.readLine()).toUpperCase();
int i=0,j=0,t;
String temp;
s=s.replace('.',' ');
s=s.replace('?',' ');
String a[]=s.split(" ");
int l=a.length;
int c[]=new int[l];
System.out.println("\nTotal number of words: "+l);
System.out.println("WORD"+"\t\t"+"FREQUENCY");
for(i=0;i<l;i++)
{
c[i]=1;
for(j=i+1;j<l;j++)
{
if(a[i].equals(a[j]))
{
c[i]++;
a[j]="";
}
}
}
for(i=0;i<l;i++)
{
if(a[i].equals(""))
{}
else
{
for(j=0;j<l;j++)

33

33

COMPUTER SCIENCE ASSIGNMENT


{
if(c[i]<c[j])
{
t=c[i];
c[i]=c[j];
c[j]=t;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
for(i=0;i<l;i++)
{
if(a[i].equals(""))
{}
else
{
System.out.println(a[i]+"\t\t\t"+c[i]);
}
}
}
}
}

34

34

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

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

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

36

36

COMPUTER SCIENCE ASSIGNMENT


QUESTION 7
Write a program to input a natural number less than 1000 and display it in words.
Test your program for the given sample data and some random data.
Sample Data:
Example 1:
InputEnter a number: 29
OutputTWENTY NINE
Example 2:
InputEnter a number: 17001
OutputOUT OF RANGE
Example 3:
InputEnter a number: 119
OutputONE HUNDRED AND NINETEEN
Example 4:
InputEnter a number: 500
OutputFIVE HUNDRED

37

37

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
Algorithm for main() method
1.
2.
3.
4.
5.

Start
Create an object of the class
Call the method inputNumber()
Call the method extract()
End of main() method
Algorithm for inputNumber() method

1. Take the input from the console


2. Store the variable in an integer variable n
3. End of inputNumber() method
Algorithm for extract() method
1. Check if n<1 or n>999, if yes then
Print ''OUT OF RANGE''
Else go to step 2
2. Repeat steps 2 to 5 till n = 0
Check if i=0, if yes then
Assign units=n%10
Else check if i=1, if yes then
Assign tens=n%10
Else check if i=2, if yes then
Assign hund=n%10
3. Increase the value of i by 1
4. Assign n=n/10
5. Go to step 2
6. Check if hund>0, if yes then
Assign str=w1(hund)+ ''HUNDRED''
Check if tens>1, if yes
str1=w2(tens)
Check if tens=1, if yes
str2=w3(units)
Else
str2=w1(units)
Assign str=str+str2
Print str
7. End of extract() method
Algorithm for w1(int x) method
1. Check x with the digits 1 to 9 and assign s with the corresponding digit in words.
2. Return s
3. End of w1(int x) method

38

38

COMPUTER SCIENCE ASSIGNMENT


Algorithm for w2(int x) method
1. Check x with digits 2 to 9 an assign
s with ''TWENTY'' if x=2
s with ''THIRTY'' if x=3
s with ''FORTY'' if x=4
s with ''FIFTY'' if x=5
s with ''SIXTY'' if x=6
s with ''SEVENTY'' if x=7
s with ''EIGHTY'' if x=8
s with ''NINETY'' if x=9
2. Return s
3. End of w2(int x) method
Algorithm for w3(int x) method
1. Check x with the digits 0 to 9 and assign
s with ''TEN'' if x=0
s with ''ELEVEN'' if x=1
s with ''TWELVE'' if x=2
s with ''THIRTEEN'' if x=3
s with ''FOURTEEN'' if x=4
s with ''FIFTEEN'' if x=5
s with ''SIXTEEN'' if x=6
s with ''SEVENTEEN'' if x=7
s with ''EIGHTEEN'' if x=8
s with ''NINETEEN'' if x=9
2. Return s
3. End of w3(int x) method

39

39

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE

import java.io.*;
public class QUESTION7
{
int n;
void inputNumber()throws IOException
{
BufferedReader inp=new BufferedReader(new InputStreamReader(System.in));
System.out.println("INPUT:\t");
n=Integer.parseInt(inp.readLine());
}
public void extract()
{
String str="",str1="",str2="";
int hund=0,tens=0,units=0,i=0;
if(n<1 || n>999)
System.out.println("\nOUT OF RANGE ");
else
{
while(n!=0)
{
if(i==0)
units=n%10;
else if(i==1)
tens=n%10;
else if(i==2)
hund=n%10;
i++;
n=n/10;
}
if(hund>0)
str=w1(hund)+ " HUNDRED ";
if(tens>1)
str1= w2(tens);
if(tens==1)
str2= w3(units);
else
str2=w1(units);
if((!str.equals(""))&&(!str1.equals("") || !str2.equals("")))
str=str+ "AND ";
if(!str1.equals(""))
str=str+ str1+ " ";

40

40

COMPUTER SCIENCE ASSIGNMENT


if(!str2.equals(""))
str=str+ str2;
System.out.println("OUTPUT:\t"+str);
}
}
String w1(int x)
{
String s="";
switch(x)
{
case 1:
s="ONE";
break;
case 2:
s="TWO";
break;
case 3:
s="THREE";
break;
case 4:
s="FOUR";
break;
case 5:
s="FIVE";
break;
case 6:
s="SIX";
break;
case 7:
s="SEVEN";
break;
case 8:
s="EIGHT";
break;
case 9:
s="NINE";
break;
}
return s;
}
String w2(int x)
{

41

41

COMPUTER SCIENCE ASSIGNMENT


String s="";
switch(x)
{
case 2:
s="TWENTY";
break;
case 3:
s="THIRTY";
break;
case 4:
s="FORTY";
break;
case 5:
s="FIFTY";
break;
case 6:
s="SIXTY";
break;
case 7:
s="SEVENTY";
break;
case 8:
s="EIGHTY";
break;
case 9:
s="NINETY";
break;
}
return s;
}
String w3(int x)
{
String s="";
switch(x)
{
case 0:
s="TEN";
break;
case 1:
s="ELEVEN";
break;
case 2:

42

42

COMPUTER SCIENCE ASSIGNMENT


s="TWELVE";
break;
case 3:
s="THIRTEEN";
break;
case 4:
s="FOURTEEN";
break;
case 5:
s="FIFTEEN";
break;
case 6:
s="SIXTEEN";
break;
case 7:
s="SEVENTEEN";
break;
case 8:
s="EIGHTEN";
break;
case 9:
s="NINETEEN";
break;
}
return s;
}
public static void main(String args[]) throws IOException
{
QUESTION7 obj=new QUESTION7();
obj.inputNumber();
obj.extract();
}
}

43

43

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
n
str
str1
str2
hund
tens
units
i
x

DATA TYPE
int
String
String
String
int
int
int
int
int

DESCRIPTION
To store the input
To store the word of the number
To store the word of the number
To store the word of the number
To store the number of 100s
To store the number of 10s
To store the number of 1s
To run a loop
As a parameter to the method

44

44

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

45

45

COMPUTER SCIENCE ASSIGNMENT

QUESTION 8
Encryption is a technique of coding messages to maintain their secrecy. A String array of size
'n' where 'n' is the greater than 1 and less than 10, stores single sentences (each sentence
ends with a full stop) in each row of the array.
Write a program to accept the size of the array. Display an appropriate message if the size is
not satisfying the given condition. Define a string array of the inputted size and fill it with
sentences row-wise. Change the sentence of the odd rows with an encryption of two
characters ahead of the original character. Also change the sentence of the even rows by
storing the sentence in reverse order. Display the encrypted sentences as per the sample data
given below.
Test your program on the sample data and some random data.
Sample Data:
Example 1:
Inputn=4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OutputKV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
Example 2:
Inputn=13
OutputINVALID INPUT

46

46

COMPUTER SCIENCE ASSIGNMENT

ALGORITHM
1. Start
2. Take the number of sentence as input from the console and store it in n
3. Check if n<=1 or n>=10, if yes then
Print ''INVALID INPUT''
Else go to step 4
4. Declare a String array with the dimension n
5. Repeat steps 5 to 7 from i=0 till i=n-1
6. Take input from the console and store it in S[i]
7. Increase value of i by 1
8. Repeat steps 8 to 15 from i=0 till i=n-1
9. Check if i is even or odd
10. If i is even, then
Assign w=S[i]
(a) Repeat steps (a) to (h) from j=0 till j=w.length()
(b) Assign ch =w.charAt(j)
(c) Check if ch>=65 and ch <=88
Assign ch=ch+2
(d) Check if ch=89 or ch=90
Assign ch=ch-24
(e) Check if ch>=97 and ch<121
Assign ch=ch+2
(f) Check if ch=121 or ch=122
Assign ch=ch-24
(g) Assign c=c+ch
(h) Increase value of j by 1
11. Print c
12. If i is odd, then go to step 13
13. Extract the words of the sentence at S[i] by split() function
14. Print the words of the sentence in the reverse order
15. Increase the value of i by 1
16. Stop

47

47

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION8
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("n= ");
int n=Integer.parseInt(br.readLine());
int i=0,j=0;
int ch=0;
String w="", c="";
if(n>1 && n<10)
{
String S[]=new String[n];
for(i=0;i<n;i++)
{
System.out.print("Enter sentence "+(i+1)+" : ");
S[i]=br.readLine();
}
for(i=0;i<n;i++)
{
if(S[i].endsWith("."));
else
{
System.out.println("Sentence must end with fullstop..Enter another time
sentence"
+(i+1)+" : ");
S[i]=br.readLine();
i--;
}
}
for(i=0;i<n;i++)
{
if((i%2)==0)
{
w=S[i];
int l=w.length();
for(j=0;j<l;j++)
{
ch=w.charAt(j);
if(ch>=65 && ch<=88)
ch=ch+2;
else if(ch==89 || ch==90)
ch=ch-24;
else if(ch>=97 && ch<121)

48

48

COMPUTER SCIENCE ASSIGNMENT


ch=ch+2;
else if(ch==121 || ch==122)
ch=ch-24;
c=c+(char)ch;
}
System.out.print(c);
c="";
System.out.println();
}
else
{
String a[]=S[i].split(" ");
int l=a.length;
a[l-1]=a[l-1].substring(0,a[l-1].length()-1);
for(j=l-1;j>0;j--)
{
System.out.print(a[j]+" ");
}
System.out.println(a[0]+".");
}

}
}
else
{
System.out.println("INVALID INPUT");
}
}
}

49

49

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
n
i
j
ch
w
c
S[]
l

DATA TYPE
int
int
int
int
String
String
String array
int

DESCRIPTION
To store the number of sentences
To run a loop
To run a loop
Used in the encryption operation
Used in the encryption operation
Used in the encryption operation
To store the inputted sentences
To store the length of the sentences

50

50

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

51

51

COMPUTER SCIENCE ASSIGNMENT


QUESTION 9
Design a program which accepts your date of birth in dd mm yyyy format. Check whether the
date entered is valid or not. If it is valid, display ''VALID DATE'', also compute and display the
day number of the year for the date of birth. If it is invalid, display ''INVALID DATE'' and then
terminate the program.
Test your program for the given sample data and some random data.
Example 1:
InputEnter your date of birth in dd mm yyyy format
05
01
2010
OutputVALID DATE
5
Example 2:
InputEnter your date of birth in dd mm yyyy format
03
04
2010
OutputVALID DATE
93
Example 3:
InputEnter your date of birth in dd mm yyyy format
34
06
2010
OutputINVALID DATE

52

52

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM 9
Algorithm for takeInput() method
1. Start
2. Take d, m and y as the input from the console
3. End of takeInput() method
Algorithm for checkValidity() method
1.
2.
3.
4.
5.
6.

7.
8.
9.
10.
11.
12.

Start
Initialise days[]={31,28,31,30,31,30,31,31,30,31,30,31}
Assign dateToDays =0
Assign l=checkLeap(y)
Assign days[1]=days[1]+l
Check if m<= or m>12 or d<=0 or d>days[m-1] or y<=0, then
Print "INVALID INPUT" and stop
Else go to step 7
Repeat steps 8 and 9 from x=0 till x=m-2
Assign dateToDays= dateToDays+days[x]
Increase value of x by 1
Assign dateToDays= dateToDays+d
Print dateToDays
End of checkValidity() method
Algorithm for checkLeap(int year) method

1. Start
2. Check if year%100 0, then return 1
Check if year%400 = 0, then return 1
Else return 0
3. End of checkLeap(int year) method
Algorithm for main() method
1.
2.
3.
4.
5.

Start
Create an object of the class
Call the function takeInput()
Call the function checkValidity()
End of the main() method

53

53

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE

import java.io.*;
class QUESTION9
{
int d,m,y;
void takeInput() throws IOException
{
BufferedReader inp=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your date of birth in dd mm yyyy format:");
d=Integer.parseInt(inp.readLine());
m=Integer.parseInt(inp.readLine());
y=Integer.parseInt(inp.readLine());
}
void checkValidity()
{
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int dateToDays=0;
int l=checkLeap(y);
days[1]+=l;
if(m<=0 || m>12 || d<=0 || d>days[m-1] || y<=0 )
System.out.println("INVALID DATE");
else
{
for(int x=0;x< m-1;x++)
{
dateToDays+=days[x];
}
dateToDays+=d;
System.out.println("\nVALID DATE\n"+ dateToDays);
}
}
int checkLeap(int year)
{
if (year% 4 == 0)
{
if (year % 100 != 0)
return 1;
else if (year % 400 == 0)
return 1;
else
return 0;
}

54

54

COMPUTER SCIENCE ASSIGNMENT


else
return 0;
}
public static void main(String args[]) throws IOException
{
QUESTION9 obj=new QUESTION9();
obj.takeInput();
obj.checkValidity();
}
}

55

55

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
d
m
y
days[]
dateToDays
year

DATA TYPE
int
int
int
int
int
int

DESCRIPTION
To store the inputted date
To store the inputted month
To store the inputted year
To store the days of the months
Used in the check of the validity
As a parameter

56

56

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

57

57

COMPUTER SCIENCE ASSIGNMENT


QUESTION 10
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>=100 and n<= 3000. Display
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:
InputM=100
N=1000
OutputThe prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15
Example 2:
InputM=100
N=5000
OutputOUT OF RANGE

58

58

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM 10
1. Start
2. Take the lower limit m and upper limit n from the console
3. Check if m>3000 or n>3000 or m>n, then
Print ''OUT OF RANGE''
Else go to step 4
4. Repeat steps 5 to 9 from i=m till i=n
5. Initialise j=2 and f=0
Repeat steps (a) and (b) from j=2 till j=i/2
(a) Check if i%j=0, then
Increase value of f by 1
(b) Increase the value of j by 1
6. Check if f=0, then
Check for i if it is palindrome.
7. If i is palindrome print i
8. Assign f=0
9. Increase value of i by 1
10. Stop

59

59

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION10
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("m=");
int m=Integer.parseInt(br.readLine());
System.out.print("n=");
int n=Integer.parseInt(br.readLine());
int f=0,i=0,j=0,c=0;
if(m<3000 && n<3000 && m<=n)
{
System.out.println("THE PRIME PALINDROME INTGERS ARE:");
for(i=m;i<=n;i++)
{
j=2;f=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
f++;
break;
}
}
if(f==0)
{
String a=Integer.toString(i);
if((((new StringBuffer(a)).reverse()).toString()).equals(a))
{
c++;
System.out.print(a+",");
}
}
}
System.out.println();
if(c>0)
System.out.println("FREQUENCY OF PRIME PALINDROME INTEGERS:"+c);
else
System.out.println("NO PRIME PALINDROME INTEGERS FOUND");
}
else
System.out.println("OUT OF RANGE");
}
}

60

60

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
n
i
j
f
c

DATA TYPE
int
int
int
int
int
int

DESCRIPTION
To store the lower limit
To store the upper limit
To run a loop
To run a nested loop
To find the nos. of factors of a number
To find the number of prime palindrome integers

61

61

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

62

62

COMPUTER SCIENCE ASSIGNMENT


QUESTION 11
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:
1. Obtain the length of the sentence (measured in words).
2. Arrange the sentence in alphabetical order of the words.
Test your program with the sample data and some random data:
Example 1:
InputNECESSITY IS THE MOTHER OF INVENTION.
OutputLENGTH: 6
REARRANGED SENTENCE:
INVENTION IS MOTHER NECESSITY OF THE
Example 2:
InputBE GOOD TO OTHERS.
OutputLENGTH: 4
REARRANGED SENTENCE:
BE GOOD OTHERS TO

63

63

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM 11
1. Start
2. Take a sentence in upper case as input from the console
3. Check if the characters of the sentence are in upper case, if not
Print ''INVALID INPUT''
4. Check if the last character of the sentence is either '.' or '!' or '?', if yes then
eliminate it using substring() function
5. Separate the words of the sentence and store it in a String array
6. Using bubble sorting technique, arrange the words in the ascending order of their
alphabets
7. Print the rearranged sentence
8. Stop

64

64

COMPUTER SCIENCE ASSIGNMENT


import java.io.*;
class QUESTION11
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence:");
String s1=(br.readLine());
int ls=s1.length();
int i=0;
for(i=0;i<ls;i++)
{
if(Character.isLowerCase(s1.charAt(i)))
{
System.out.println("INVALID INPUT.");
System.exit(0);
}
System.out.println();
if(s1.charAt(ls-1)=='.' || s1.charAt(ls-1)=='!' || s1.charAt(ls-1)=='?')
s1=s1.substring(0,s1.length()-1);
String a[]=s1.split(" ");
int l=a.length;
String temp;
int j;
for(i=0;i<l;i++)
{
for(j=0;j<(l-i-1);j++)
{
if(a[j].charAt(0)>a[j+1].charAt(0))
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("LENGTH: "+l);
System.out.println("REARRANGED SENTENCE"+"\n");
for(i=0;i<l;i++)
System.out.print(a[i]+" ");
}
}
}

65

65

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
s1
ls
i
a[]
i
temp
j

DATA TYPE
String
int
int
String array
int
String
int

DESCRIPTION
To store the inputted sentence
To store the length of the sentence
To run loop
To store the words of the sentence
To store the length of the sentence in words
Temporary variable to be used in the sorting
To run loop

66

66

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

67

67

COMPUTER SCIENCE ASSIGNMENT


QUESTION 12
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 both M must be greater than 2 and less than 20.
Allow the user to input integers into this matrix. Perform the following tasks on the matrix:
1. Display the input matrix
2. Create a mirror image matrix
3. Display the mirror image matrix
Test your program with the sample data and some random data:
InputM=3
4
16

12

14

OutputORIGINAL MATRIX
4

16

12

14

MIRROR IMAGE MATRIX


12

16

14

68

68

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take the size of the matrix M as the input from console and store it in m
3. Check if m<=2 or m>=10, then print "SIZE OUT OF RANGE"
Else go to step 4
4. Declare a matrix M as M[][]=new int[m][m]
5. Repeat steps 5 to 9 from i=0 till i=m-1
6. Repeat steps 6 to 8 from j=0 till j=m-1
7. Take the element [i][j] of the matrix M as input from the console
8. Increase the value of j by 1
9. Increase the value of i by 1
10. Repeat steps 10 to 14 from i=0 till i=m-1
11. Repeat steps 11 to 13 from j=0 till j=m-1
12. Print M[i][j]
13. Increase the value of j by 1
14. Increase the value of i by 1
15. Declare an array as A[][]=new int[m][m]
16. Repeat steps 16 to 20 from i=0 till i=m-1
17. Repeat steps 17 to 19 from j=0 till j=m-1
18. Assign A[m-i-1][j]=M[i][j]
19. Increase the value of j by 1
20. Increase the value of i by 1
21. Repeat steps 21 to 25 from i=0 till i=m-1
22. Repeat steps 22 to 24 from j=0 till j=m-1
23. Print A[i][j]
24. Increase the value of j by 1
25. Increase the value of i by 1
26. Stop

69

69

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class Question12
{
public static void main(String args[])throws IOException
{
int m, i, j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("M= ");
m=Integer.parseInt(br.readLine());
if(m<=2 || m>=10)
System.out.println("SIZE OUT OF RANGE");
else
{
int M[][]=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print("Enter element ["+i+"]["+j+"]:");
M[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(M[i][j]+"\t");
System.out.println();
}
int A[][]=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
A[m-i-1][j]=M[i][j];
}
System.out.println("MIRROR IMAGE MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
}
}
}

70

70

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
i
j
M[][]
A[][]

DATA TYPE
int
int
int
int array
int array

DESCRIPTION
To store the matrix size
To run a loop
To run a loop
To store the original matrix
To store the mirror image matrix

71

71

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

72

72

COMPUTER SCIENCE ASSIGNMENT


QUESTION 13
An ISBN (International Standard Book Number) is a ten-digit code which uniquely identifies a
book.
The first nine digits represent the Group, Publisher and Title of the book and the last digit is
used to check whether ISBN is correct or not.
Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is
necessary to make the last digit equal
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, 8 times the
third digit and so on until we add 1 time the last digit. If the final number leaves no remainder
when divided by 11, the code is a valid ISBN.
For example:
1. 0201103311=10*0+9*2+8*0+7*1+6*1+5*0+4*3+3*3+2*1+1*1=55.
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
2. 0112112425=10*0+9*1+8*1+7*2+6*1+5*1+4*1+3*4+2*2+1*5=71.
Since 71 leaves remainder when divided by11, hence it is not a valid ISBN.
Design a program to accept a ten-digit code from the user. For an invalid input, display an
appropriate message. Verify the code for its validity in the format specified below:
Test your program with the sample data and some random data:
Example 1:
Input
Output

0201530821
SUM=99
LEAVES NO REMAINDER VALID ISBN CODE

Example 2:
Input
Output

035680324
INVALID INPUT

Example 1:
Input
Output

0231428031
SUM=122
LEAVES REMAINDER INVALID ISBN CODE

73

73

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take the code from the console as input and store it in n1
3. Check the length of n1 and store it in l
Check if l 10, then print ''INVALID INPUT'' and stop
4. Check if the first 9 characters of the code are digits.
If not print ''INVALID INPUT'' and stop
5. Repeat steps 6 to 8 from i=0 till i=8
6. Assign char ch as ch=n1.charAt(i)
7. Assign int s as s=s+((ch-24)*10)
8. Increase the value of I by 1
9. Check if the last character of the code is 'X'
10. If yes then assign s=s+10
11. Else assign s=s+(it value of the last character)
12. Check for divisibility of s by 11
13. If divisible print ''VALID ISBN''
14. If not print ''INVALID ISBN''
15. Stop

74

74

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class Question13
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("INPUT CODE:\t");
String n1=br.readLine();
int l=n1.length();
int c1=0,s=0;
if(l!=10)
{
System.out.println("INVALID INPUT");
System.exit(0);
}
else
{
for(int i=0;i<9;i++)
{
char ch=n1.charAt(i);
if(Character.isDigit(ch)==false)
{
System.out.println("INVALID INPUT");
System.exit(0);
}
s=s+((ch-48)*(10-i));
}
char ch1=n1.charAt(9);
if(ch1=='X')
s=s+10;
else if(Character.isDigit(ch1)==true)
s=s+(ch1-48);
else
{
System.out.println("INVALID INPUT");
System.exit(0);
}
System.out.println("SUM = "+s);
if((s%11)==0)
System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE");
else
System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");
}
}
}

75

75

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
i
j
M[][]
A[][]

DATA TYPE
int
int
int
int array
int array

DESCRIPTION
To store the matrix size
To run a loop
To run a loop
To store the original matrix
To store the mirror image matrix

76

76

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

77

77

COMPUTER SCIENCE ASSIGNMENT


QUESTION 14
A Palindrome is a word that may be read the same way in either direction. Accept a sentence
in UPPER CASE which is terminated by either '.', '!' or '?'. Each word of the sentence is
separated by a single blank space. Perform the following tasks:
(a) Display the count of palindromic words in the sentence.
(b) Display the palindromic words in the sentence.
Example of palindromic words:
MADAM, ARORA, NOON
Test your program with the sample data and some random data:
Example 1:
Input
Output

MOM AND DAD ARE COMING AT NOON.


MOM DAD NOON
NUMBER OF PALINDROMIC WORDS: 3

Example 2:
Input
Output

NITIN ARORA USES LIRIL SOAP.


NITIN ARORA LIRIL
NUMBER OF PALINDROMIC WORDS: 3

Example 3:
Input
Output

HOW ARE YOU?


NO PALINDROMIC WORDS

78

78

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take a sentence as input from the console store it in String s1
3. Check if the last character of the sentence is either '.' or '!' or '?', if yes then
eliminate it using substring() function
Else print ''INVALID INPUT''
4. Separate the words of the string using split function and store them in a String
array a[]
5. Assign l=a.length
6. Repeat steps 6 to 9 from i=0 till i=l-1
7. Reverse the word stored in a[i] using StringBuffer function
8. Check if the reversed word is equal to the original word
If yes then print a[i] and increase value of c by 1
9. Increase value of i by 1
10. Stop

79

79

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class Question14
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("INPUT A SENTENCE:");
String s1=(br.readLine()).toUpperCase();
int ls=s1.length();
if(s1.charAt(ls-1)!='.' && s1.charAt(ls-1)!='?' && s1.charAt(ls-1)!='!')
{
System.out.println("INVALID INPUT.");
System.exit(0);
}
else
{
s1=s1.substring(0,s1.length()-1);
String a[]=s1.split(" ");
int l=a.length,i=0,c=0;
for(i=0;i<l;i++)
{
String sr=(((new StringBuffer(a[i])).reverse()).toString());
if(sr.equalsIgnoreCase(a[i]))
{
System.out.print(sr+"\t");
c++;
}
}
System.out.println();
if(c==0)
System.out.println("NO PALINDROMIC WORDS");
else
System.out.println("NUMBER OF PALINDROMIC WORDS :"+"\t"+c);
}
}
}

80

80

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
s1
l
i
c
sr
a[]

DATA TYPE
String
int
int
int
String
String array

DESCRIPTION
To store the inputted sentence
To store the length of the sentence
To run a loop
To store the number of palindrome words
To store the reversed words
To store the words of the sentence

81

81

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

82

82

COMPUTER SCIENCE ASSIGNMENT


QUESTION 15
A Composite Magic number is a positive integer which is composite as well as a magic
number.
Composite number:

A composite number is a number that has more than two factors.


For example: 10
Factors are: 1, 2, 5, 10

Magic number:

A magic number is a number in which the eventual sum of the digits


is equal to 1.
For example: 28=2+8=10=1+0=1

Accept two integers m and n, where m is less than n, as user input. Display the number of
Composite magic integers that are in the range between m and n (both inclusive) and output
them along with the frequency, in the format specified below.
Test your program with the sample data and some random data:
Example 1:
Input -

m=10
n=100

Output THE COMPOSITE MAGIC INTEGERS ARE:


10, 28, 46, 55, 64, 82, 91, 100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8
Example 1:
Input -

m=1200
n=1300

Output THE COMPOSITE MAGIC INTEGERS ARE:


1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9
Example 1:
Input -

m=120
n=99

Output INVALID INPUT

83

83

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

15.
16.
17.
18.

Start
Taker the lower limit m and upper limit n as input from the console
Check if m>n, then print ''INVALID INPUT'' and stop
Repeat steps 5 to 17 from i=m till i=n
Initialise a=i
Repeat steps 6 to 8 from j=2 till j=a/2
Check if a%j=0, then increase value of f by 1
Increase value of j by 1
Check if f 0, thengo to step 10 else go to step 17
Repeat steps 10 to 15 when a>0
Assign r=a%10
Assign a=a/10
Assign s=s+r
Check if a=0 and s>9, then
Assign a=s
Assign s=0
Goto step 10
If s=1, print i
Increase value of I by 1
Stop

84

84

COMPUTER SCIENCE ASSIGNMENT


import java.io.*;
class Question15
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("ENTER m AND n: ");
int m=Integer.parseInt(br.readLine()); int n=Integer.parseInt(br.readLine());
int i=0,f=0,j=1,r=0,a=0,s=0,c=0;
if(m>n)
System.out.println("Invalid Input.");
else
{ System.out.println("THE COMPOSITE MAGIC NUMBERS ARE:");
for(i=m;i<=n;i++)
{
a=i;
for(j=2;j<=a/2;j++)
{
if((a%j)==0)
{
f++;
break;
}
}
if(f!=0)
{
s=0;
a=i;
while(a>0)
{
r=a%10; a=a/10;
s=s+r;
if(a==0 && s>9)
{
a=s;
s=0;
}
if(s==1)
{ System.out.print(i+",");
c++;
}
f=0;
} System.out.print("\n"+"FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: "+c);
}
}
}

85

85

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
n
i
f
j
r
a
s
c

DATA TYPE
int
int
int
int
int
int
int
int
int

DESCRIPTION
To store the lower limit
To store the upper limit
To run loop
To store the nos. of factors of a number
To run a loop
To extract the digit
Dummy variable
To store the eventual sum of the digits
To count the nos. of composite magic numbers

86

86

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

87

87

COMPUTER SCIENCE ASSIGNMENT


QUESTION 16
Write a program to declare a square matrix A[][] of order (M x M) where 'M' is the number of
rows and the number of columns such that M must be greater than 2 and less than 10. Accept
the value of M as user input. Display an appropriate message for an invalid input. Allow the
user to input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Check if the given matrix is symmetric or not. A square matrix is said to be symmetric,
if the element of the ith row and jth column is equal to the element of jth row and ith
column.
(c) Find the sum of the elements of the left diagonal and the sum of the right diagonal of
the matrix and display them.
Test your program with the sample data and some random data:
Example 1:
Input- M= 3
1
2
3
2

3
5
OutputORIGINAL MATRIX
1
2

3
5
6
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal=11
The sum of the right diagonal=10
Example 2:
Input- M= 4
7
8
9
2
4

OutputORIGINAL MATRIX
7
8

7
6
4
2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal=17
The sum of the right diagonal=20

88

88

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take the size of the matrix M as the input from console and store it in m
3. Check if m<=2 or m>=10, then print "SIZE OUT OF RANGE"
Else go to step 4
4. Declare a matrix M as M[][]=new int[m][m]
5. Repeat steps 5 to 9 from i=0 till i=m-1
6. Repeat steps 6 to 8 from j=0 till j=m-1
7. Take the element [i][j] of the matrix M as input from the console
8. Increase the value of j by 1
9. Increase the value of i by 1
10. Repeat steps 10 to 14 from i=0 till i=m-1
11. Repeat steps 11 to 13 from j=0 till j=m-1
12. Print M[i][j]
13. Increase the value of j by 1
14. Increase the value of i by 1
15. Repeat steps 15 to 19 from i=0 till i=m-1
16. Repeat steps 16 to 18 from j=0 till j=m-1
17. Check if M[i][j]=M[j][i], then increase the value of c by 1
18. Increase the value of j by 1
19. Increase the value of i by 1
20. Check if c=m*m, then print "THE GIVEN MATRIX IS SYMMETRIC"
Else print "THE GIVEN MATRIX IS NOT SYMMETRIC"
21. Repeat steps 22 and 23 from i=0 till i=m-1
22. Assign ls=ls+M[i][i]
23. Increase the value of i by 1
24. Repeat steps 25 and 26 from j=0 till j=m-1
25. Assign rs=rs+M[j][m-j-1]
26. Increase the value of j by 1
27. Print ls
28. Print rs
29. Stop

89

89

COMPUTER SCIENCE ASSIGNMENT


import java.io.*;
class Question16
{
public static void main(String args[])throws IOException
{
int m, n, i, j, c=0, ls=0, rs=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter m: ");
m=Integer.parseInt(br.readLine());
int M[][]=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print("Enter element ["+i+"]["+j+"]:");
M[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(M[i][j]+"\t");
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(M[i][j]==M[j][i])
c++;
}
}
if(c!=m*m)
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
else
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
for(i=0;i<m;i++)
ls=ls+M[i][i];
System.out.println("The sum of the left diagonal = "+ls);
for(j=0;j<m;j++)
rs=rs+M[j][m-j-1];
System.out.println("The sum of the right diagonal = "+rs);
}
}

90

90

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
i
j
M[][]
c
ls
rs

DATA TYPE
int
int
int
int array
int
int
int

DESCRIPTION
To store the size of the matrix
To run a loop
To run a loop
To store the inputted elements
Used as a counter
To store the sum of the left diagonal elements of the matrix
To store the sum of right diagonal elements of the matrix

91

91

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

92

92

COMPUTER SCIENCE ASSIGNMENT


QUESTION 17
Given two positive numbers M and N, such that M is between 100 and 10000 and N is less
than 100. Find the smallest integer that is greater than M and whose digits add up to N. For
example, if M= 100 and N= 11, then the smallest integer greater than 100 whose digits add up
to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest required
number whose sum of all its digits is equal to N. Also, print the total number of digits present
in the required number. The program should check for the validity of the inputs and display
an appropriate message for an invalid input.
Example 1:
InputOutputExample 2:
InputOutputExample 3:
InputOutputExample:
InputOutput-

M= 100
N=11
The required number= 119
Total number of digits= 3
M= 1500
N= 25
The required number= 1699
Total number of digits= 4
M= 99
N=11
INVALID INPUT
M= 112
N=130
INVALID INPUT

93

93

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

Start
Take M and N as user input
Check if m<100 or m>10000 or n>=100, then print ''INVALID INPUT''
Initialise s=0
Repeat steps 6 to 12 when s n
Assign a=m, s=0
Repeat steps 8 to 11 when a>0
Assign r=a%10
Assign a=a/10
Assign s=s+r
Go to step 7
Increase the value of i by 1
Assign k=m-1
Print k
Stop

94

94

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class Question17
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("M = ");
int m=Integer.parseInt(br.readLine());
System.out.print("N = ");
int n=Integer.parseInt(br.readLine());
int a,s=0,r;
if(m<100 || m>10000 || n>=100)
System.out.println("INVALID INPUT");
else
{
do{
a=m;
s=0;
while(a>0)
{
r=a%10;
a=a/10;
s=s+r;
}
m++;
}while(s!=n);
System.out.println("The required number= "+(m-1));
String s1=Integer.toString(m-1);
System.out.println("Total number of digits= "+s1.length());
}
}
}

95

95

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
n
a
s
r

DATA TYPE
int
int
int
int
int

DESCRIPTION
To store M
To store N
Dummy variable
Used in the operation
To be used in the extraction of digits of M

96

96

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

97

97

COMPUTER SCIENCE ASSIGNMENT


QUESTION 18
Write a program to declare a square matrix A[][] of order (M x M) where 'M' is the number of
rows and the number of columns such that M must be greater than 2 and less than 10. Accept
the value of M as user input. Display an appropriate message for an 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 clockwise as shown below:
Original Matrix
Rotated Matrix
1 2 3
7 4 1
4 5 6
8 5 2
7 8 9
9 6 3
(c) Find the sum of the elements of the four corners of the matrix.
Test your program with the sample data and some random data:
Example 1:
Input-

M= 3
3

1
6
MATRIX AFTER ROTATION
1
2

OutputORIGINAL MATRIX
3
2

3
4

7
8
9
Sum of the corner elements= 20
Example 2:
InputOutput-

M= 14
SIZE OUT OF RANGE

98

98

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take the size of the matrix M as the input from console and store it in m
3. Check if m<=2 or m>=10, then print "SIZE OUT OF RANGE"
Else go to step 4
4. Declare a matrix M as M[][]=new int[m][m]
5. Repeat steps 5 to 9 from i=0 till i=m-1
6. Repeat steps 6 to 8 from j=0 till j=m-1
7. Take the element [i][j] of the matrix M as input from the console
8. Increase the value of j by 1
9. Increase the value of i by 1
10. Repeat steps 10 to 14 from i=0 till i=m-1
11. Repeat steps 11 to 13 from j=0 till j=m-1
12. Print M[i][j]
13. Increase the value of j by 1
14. Increase the value of i by 1
15. Repeat steps 15 to 19 from i=0 till i=m-1
16. Repeat steps 16 to 18 from j=m-1 till j=0
17. Print M[j][i]
18. Decrease the value of j by 1
19. Increase the value of i by 1
20. Assign s=M[0][0]+M[0][m-1]+M[m-1][0]+M[m-1][m-1]
21. Print s
22. Stop

99

99

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class Question18
{
public static void main(String args[])throws IOException
{
int m, n, i, j, s=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("M= ");
m=Integer.parseInt(br.readLine());
if(m<=2 || m>=10)
System.out.println("SIZE OUT OF RANGE");
else
{
int M[][]=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print("Enter element ["+i+"]["+j+"]:");
M[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(M[i][j]+"\t");
}
System.out.println();
}
System.out.println("MATRIX AFTER ROTATION: ");
for(i=0;i<m;i++)
{
for(j=m-1;j>=0;j--)
{
System.out.print(M[j][i]+"\t");
}
System.out.println();
}
s=M[0][0]+M[0][m-1]+M[m-1][0]+M[m-1][m-1];
System.out.println("Sum of the corner elements= "+s);
}
}
}

100

10

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
i
j
M[][]
s

DATA TYPE
int
int
int
int array
int

DESCRIPTION
To store the size of the matrix
To run a loop
To run a loop
To store the inputted elements
To store the sum of the corner elements

101

10

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

102

10

Вам также может понравиться