8/20/2019 Java in BlueJ Project
1/102
COMPUTER SCIENCE ASSIGNMENT
1
1
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
8/20/2019 Java in BlueJ Project
2/102
COMPUTER SCIENCE ASSIGNMENT
2
2
ALGORITHM
Algorithm for main() method
1.
Start
2.
Take day as input from console and store it in int variable d1
3.
Take year as input from console and store it in int variable y1
4.
Take N as input from console and store it in int variable n
5.
Call the function Date as: obj.Date(d1,y1)
6.
Check if n>=1 and na[i]
7.
Assign d=d−a[i]
8.
Increase value of i by 1
9.
Assign j=i
10.
Repeat step 11 for i=0 till i
8/20/2019 Java in BlueJ Project
3/102
COMPUTER SCIENCE ASSIGNMENT
3
3
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=1 && n366)
{
y1+=1;
d1-=366;
8/20/2019 Java in BlueJ Project
4/102
COMPUTER SCIENCE ASSIGNMENT
4
4
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");
}
}
8/20/2019 Java in BlueJ Project
5/102
COMPUTER SCIENCE ASSIGNMENT
5
5
DATA VARIABLE DESCRIPTION
NAME DATA TYPE DESCRIPTION
d int As an argument for the function Date()
y Int As an argument for the function Date()
a[] int array An array to store the nos. of days of the months of a year
m[] String array An array to store the name of the months of a year
i int To run a loop
j int To store the month number
d1 int To store the inputted day number
y1 Int To store the inputted year
n int To store the inputted N
8/20/2019 Java in BlueJ Project
6/102
COMPUTER SCIENCE ASSIGNMENT
6
6
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
7/102
COMPUTER SCIENCE ASSIGNMENT
7
7
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:
Input-
m=3
n=3
Original matrix: 1 7 4
8 2 5
6 3 9
Rearranged matrix: 1 3 4
9 2 5
8 7 6
Sum=43
8/20/2019 Java in BlueJ Project
8/102
8/20/2019 Java in BlueJ Project
9/102
COMPUTER SCIENCE ASSIGNMENT
9
9
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
8/20/2019 Java in BlueJ Project
10/102
COMPUTER SCIENCE ASSIGNMENT
10
1
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=20 || n=20)
{
System.out.println("OUT OF RANGE");
System.exit(0);
}
int A[][]=new int[m][n];
for(i=0;i
8/20/2019 Java in BlueJ Project
11/102
8/20/2019 Java in BlueJ Project
12/102
COMPUTER SCIENCE ASSIGNMENT
12
12
}
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
8/20/2019 Java in BlueJ Project
13/102
COMPUTER SCIENCE ASSIGNMENT
13
13
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store the number of rows
n int To store the number of columns
i int To run loops
j int To run loops
A[][] int array To store the elements inputted by user
no int To store the number of boundary elements
e[] int array To store the boundary elements
k int To store the number of boundary elements
t int To use as a temporary variable during sorting
sum int To store the sum of the boundary element
8/20/2019 Java in BlueJ Project
14/102
COMPUTER SCIENCE ASSIGNMENT
14
14
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
15/102
COMPUTER SCIENCE ASSIGNMENT
15
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.
8/20/2019 Java in BlueJ Project
16/102
8/20/2019 Java in BlueJ Project
17/102
COMPUTER SCIENCE ASSIGNMENT
17
17
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
8/20/2019 Java in BlueJ Project
18/102
COMPUTER SCIENCE ASSIGNMENT
18
18
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
s1 String To store the inputted string
a[] String array To store the words of the stringl int To store the number of words
temp int Temporary variable to be used during sorting
i int To run a loop
j int To run a loop
8/20/2019 Java in BlueJ Project
19/102
COMPUTER SCIENCE ASSIGNMENT
19
19
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
20/102
COMPUTER SCIENCE ASSIGNMENT
20
2
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 descendingorder 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
OUTPUT-
ONE 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
OUTPUT-
SIX ZERO FOUR THREE
DENOMINATORS:
1000X6=6000
20X2=40
2X1=2
1X1=1
TOTAL NUMBER OF NOTES: 10
Example 3:
INPUT- 235001
OUTPUT-
Input out of range.
8/20/2019 Java in BlueJ Project
21/102
COMPUTER SCIENCE ASSIGNMENT
21
21
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
8/20/2019 Java in BlueJ Project
22/102
COMPUTER SCIENCE ASSIGNMENT
22
22
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
8/20/2019 Java in BlueJ Project
23/102
COMPUTER SCIENCE ASSIGNMENT
23
23
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);
}
}
8/20/2019 Java in BlueJ Project
24/102
COMPUTER SCIENCE ASSIGNMENT
24
24
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
rev int To store the number of notes of a particular denomination
amount int To store the inputted amount
amt int Dummy variable
a String To store the inputted amount
ch char To store the extracted digits of the inputted amount
den[] int array To store the available denominations
i int To run a loop
tot int To store the total number of notes
8/20/2019 Java in BlueJ Project
25/102
COMPUTER SCIENCE ASSIGNMENT
25
25
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
26/102
COMPUTER SCIENCE ASSIGNMENT
26
26
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
8/20/2019 Java in BlueJ Project
27/102
COMPUTER SCIENCE ASSIGNMENT
27
27
Algorithm
1.
Start
2.
Take p as input from console and store bit in int variable p
3.
Take q as input from console and store bit in int variable q
4.
Initialise two int variable as i=0 and c=05.
Check if p>5000 or q>5000 or p>q, if yes then
Print "OUT OF RANGE"
Else
Goto step 6
6.
Repeat steps 7 to 12 for i=p till i=q
7.
Assign int len with the length of i as len=(""+i).length()
8.
Assign long sq=i*i
9.
Assign long k=sq%(10^len)
10. Assign long s=sq/(10^len)
11. Assign long newn=k+s
12.
Check if newn=I, if yes then
Print i
Increase value of c by 1
13. Print c
14. End
8/20/2019 Java in BlueJ Project
28/102
COMPUTER SCIENCE ASSIGNMENT
28
28
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
8/20/2019 Java in BlueJ Project
29/102
COMPUTER SCIENCE ASSIGNMENT
29
29
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
p int To store the lower limit
q int To store the upper limit
i int To run a loop
c int Counter to count the number of Kaprekar numbers
len long To store the length of each number
sq long To store the square of each number
r long To store the right hand piece of the square of the number
l long To store the left hand piece of the square of the number
newn long To store the new number created by adding r and l
8/20/2019 Java in BlueJ Project
30/102
COMPUTER SCIENCE ASSIGNMENT
30
3
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
31/102
COMPUTER SCIENCE ASSIGNMENT
31
31
QUESTION 6
Input a paragraph containing 'n' number of sentences where (1=
8/20/2019 Java in BlueJ Project
32/102
COMPUTER SCIENCE ASSIGNMENT
32
32
ALGORITHM
1.
Start
2.
Take the number of sentences as input from console and store it in int variable n
3.
Check if 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
8/20/2019 Java in BlueJ Project
33/102
COMPUTER SCIENCE ASSIGNMENT
33
33
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=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
8/20/2019 Java in BlueJ Project
34/102
COMPUTER SCIENCE ASSIGNMENT
34
34
{
if(c[i]
8/20/2019 Java in BlueJ Project
35/102
COMPUTER SCIENCE ASSIGNMENT
35
35
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
n int To store the number of sentences
s String To store the sentences
i int To run a loop
j int To run a loop
t int Temporary variable used during sorting
temp String Temporary variable used during sorting
a[] String array Array to store the words
l int To store the number of words
c[] int array Array to store the length of each word
8/20/2019 Java in BlueJ Project
36/102
COMPUTER SCIENCE ASSIGNMENT
36
36
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
37/102
COMPUTER SCIENCE ASSIGNMENT
37
37
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:
Input-
Enter a number: 29
Output-
TWENTY NINE
Example 2:
Input-
Enter a number: 17001
Output-
OUT OF RANGE
Example 3:
Input-
Enter a number: 119
Output-
ONE HUNDRED AND NINETEEN
Example 4:
Input-
Enter a number: 500
Output-FIVE HUNDRED
8/20/2019 Java in BlueJ Project
38/102
COMPUTER SCIENCE ASSIGNMENT
38
38
ALGORITHM
Algorithm for main() method
1. Start
2. Create an object of the class
3.
Call the method inputNumber()4. Call the method extract()
5. 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 n999, 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 14.
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
8/20/2019 Java in BlueJ Project
39/102
COMPUTER SCIENCE ASSIGNMENT
39
39
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
8/20/2019 Java in BlueJ Project
40/102
COMPUTER SCIENCE ASSIGNMENT
40
4
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(n999)
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+ " ";
8/20/2019 Java in BlueJ Project
41/102
COMPUTER SCIENCE ASSIGNMENT
41
41
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)
{
8/20/2019 Java in BlueJ Project
42/102
COMPUTER SCIENCE ASSIGNMENT
42
42
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:
8/20/2019 Java in BlueJ Project
43/102
COMPUTER SCIENCE ASSIGNMENT
43
43
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();
}
}
8/20/2019 Java in BlueJ Project
44/102
COMPUTER SCIENCE ASSIGNMENT
44
44
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
n int To store the input
str String To store the word of the numberstr1 String To store the word of the number
str2 String To store the word of the number
hund int To store the number of 100s
tens int To store the number of 10s
units int To store the number of 1s
i int To run a loop
x int As a parameter to the method
8/20/2019 Java in BlueJ Project
45/102
COMPUTER SCIENCE ASSIGNMENT
45
45
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
46/102
COMPUTER SCIENCE ASSIGNMENT
46
46
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:Input-
n=4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
Output-
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
Example 2:
Input-
n=13
Output-
INVALID INPUT
8/20/2019 Java in BlueJ Project
47/102
COMPUTER SCIENCE ASSIGNMENT
47
47
ALGORITHM
1. Start
2. Take the number of sentence as input from the console and store it in n
3.
Check if n=10, if yes thenPrint ''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 =97 and ch
8/20/2019 Java in BlueJ Project
48/102
COMPUTER SCIENCE ASSIGNMENT
48
48
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
8/20/2019 Java in BlueJ Project
49/102
COMPUTER SCIENCE ASSIGNMENT
49
49
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");
}
}
}
8/20/2019 Java in BlueJ Project
50/102
COMPUTER SCIENCE ASSIGNMENT
50
5
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
n int To store the number of sentences
i int To run a loop j int To run a loop
ch int Used in the encryption operation
w String Used in the encryption operation
c String Used in the encryption operation
S[] String array To store the inputted sentences
l int To store the length of the sentences
8/20/2019 Java in BlueJ Project
51/102
COMPUTER SCIENCE ASSIGNMENT
51
51
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
52/102
COMPUTER SCIENCE ASSIGNMENT
52
52
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:
Input-
Enter your date of birth in dd mm yyyy format
05
01
2010
Output-
VALID DATE
5
Example 2:
Input-
Enter your date of birth in dd mm yyyy format
03
04
2010
Output-
VALID DATE
93
Example 3:Input-
Enter your date of birth in dd mm yyyy format
34
06
2010
Output-
INVALID DATE
8/20/2019 Java in BlueJ Project
53/102
COMPUTER SCIENCE ASSIGNMENT
53
53
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.
Start
2.
Initialise days[]={31,28,31,30,31,30,31,31,30,31,30,31}
3.
Assign dateToDays =0
4.
Assign l=checkLeap(y)
5.
Assign days[1]=days[1]+l
6.
Check if m12 or ddays[m-1] or y
8/20/2019 Java in BlueJ Project
54/102
COMPUTER SCIENCE ASSIGNMENT
54
54
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(m12 || ddays[m-1] || y
8/20/2019 Java in BlueJ Project
55/102
COMPUTER SCIENCE ASSIGNMENT
55
55
else
return 0;
}
public static void main(String args[]) throws IOException
{
QUESTION9 obj=new QUESTION9();
obj.takeInput();
obj.checkValidity();
}
}
8/20/2019 Java in BlueJ Project
56/102
COMPUTER SCIENCE ASSIGNMENT
56
56
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
d int To store the inputted date
m int To store the inputted monthy int To store the inputted year
days[] int To store the days of the months
dateToDays int Used in the check of the validity
year int As a parameter
8/20/2019 Java in BlueJ Project
57/102
COMPUTER SCIENCE ASSIGNMENT
57
57
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
58/102
COMPUTER SCIENCE ASSIGNMENT
58
58
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=100 and n
8/20/2019 Java in BlueJ Project
59/102
COMPUTER SCIENCE ASSIGNMENT
59
59
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
8/20/2019 Java in BlueJ Project
60/102
8/20/2019 Java in BlueJ Project
61/102
COMPUTER SCIENCE ASSIGNMENT
61
61
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store the lower limit
n int To store the upper limiti int To run a loop
j int To run a nested loop
f int To find the nos. of factors of a number
c int To find the number of prime palindrome integers
8/20/2019 Java in BlueJ Project
62/102
COMPUTER SCIENCE ASSIGNMENT
62
62
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
63/102
8/20/2019 Java in BlueJ Project
64/102
COMPUTER SCIENCE ASSIGNMENT
64
64
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
8/20/2019 Java in BlueJ Project
65/102
COMPUTER SCIENCE ASSIGNMENT
65
65
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
8/20/2019 Java in BlueJ Project
66/102
COMPUTER SCIENCE ASSIGNMENT
66
66
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
s1 String To store the inputted sentence
ls int To store the length of the sentencei int To run loop
a[] String array To store the words of the sentence
i int To store the length of the sentence in words
temp String Temporary variable to be used in the sorting
j int To run loop
8/20/2019 Java in BlueJ Project
67/102
COMPUTER SCIENCE ASSIGNMENT
67
67
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
68/102
COMPUTER SCIENCE ASSIGNMENT
68
68
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:
Input-
M=3
4 16 12
8 2 14
4 1 3
Output-
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 4
8/20/2019 Java in BlueJ Project
69/102
COMPUTER SCIENCE ASSIGNMENT
69
69
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=10, then print "SIZE OUT OF RANGE"
Else go to step 44.
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
8/20/2019 Java in BlueJ Project
70/102
COMPUTER SCIENCE ASSIGNMENT
70
7
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=10)
System.out.println("SIZE OUT OF RANGE");
else
{
int M[][]=new int[m][m];
for(i=0;i
8/20/2019 Java in BlueJ Project
71/102
COMPUTER SCIENCE ASSIGNMENT
71
71
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store the matrix size
i int To run a loop j int To run a loop
M[][] int array To store the original matrix
A[][] int array To store the mirror image matrix
8/20/2019 Java in BlueJ Project
72/102
COMPUTER SCIENCE ASSIGNMENT
72
72
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
73/102
COMPUTER SCIENCE ASSIGNMENT
73
73
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 - 0201530821
Output - SUM=99
LEAVES NO REMAINDER – VALID ISBN CODE
Example 2:
Input - 035680324
Output - INVALID INPUT
Example 1:
Input - 0231428031
Output - SUM=122
LEAVES REMAINDER – INVALID ISBN CODE
8/20/2019 Java in BlueJ Project
74/102
COMPUTER SCIENCE ASSIGNMENT
74
74
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
8/20/2019 Java in BlueJ Project
75/102
COMPUTER SCIENCE ASSIGNMENT
75
75
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
8/20/2019 Java in BlueJ Project
76/102
COMPUTER SCIENCE ASSIGNMENT
76
76
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store the matrix size
i int To run a loop j int To run a loop
M[][] int array To store the original matrix
A[][] int array To store the mirror image matrix
8/20/2019 Java in BlueJ Project
77/102
COMPUTER SCIENCE ASSIGNMENT
77
77
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
78/102
COMPUTER SCIENCE ASSIGNMENT
78
78
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 - MOM AND DAD ARE COMING AT NOON.
Output - MOM DAD NOON
NUMBER OF PALINDROMIC WORDS: 3
Example 2:
Input - NITIN ARORA USES LIRIL SOAP.
Output - NITIN ARORA LIRIL
NUMBER OF PALINDROMIC WORDS: 3
Example 3:
Input - HOW ARE YOU?
Output - NO PALINDROMIC WORDS
8/20/2019 Java in BlueJ Project
79/102
8/20/2019 Java in BlueJ Project
80/102
COMPUTER SCIENCE ASSIGNMENT
80
8
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
8/20/2019 Java in BlueJ Project
81/102
COMPUTER SCIENCE ASSIGNMENT
81
81
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
s1 String To store the inputted sentence
l int To store the length of the sentencei int To run a loop
c int To store the number of palindrome words
sr String To store the reversed words
a[] String array To store the words of the sentence
8/20/2019 Java in BlueJ Project
82/102
COMPUTER SCIENCE ASSIGNMENT
82
82
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
83/102
COMPUTER SCIENCE ASSIGNMENT
83
83
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
8/20/2019 Java in BlueJ Project
84/102
COMPUTER SCIENCE ASSIGNMENT
84
84
ALGORITHM
1.
Start
2.
Taker the lower limit m and upper limit n as input from the console
3.
Check if m>n, then print ''INVALID INPUT'' and stop
4.
Repeat steps 5 to 17 from i=m till i=n5.
Initialise a=i
6.
Repeat steps 6 to 8 from j=2 till j=a/2
7.
Check if a%j=0, then increase value of f by 1
8.
Increase value of j by 1
9. Check if f ≠ 0, thengo to step 10 else go to step 17
10.
Repeat steps 10 to 15 when a>0
11.
Assign r=a%10
12.
Assign a=a/10
13. Assign s=s+r
14. Check if a=0 and s>9, then
Assign a=s
Assign s=0
15. Goto step 10
16. If s=1, print i
17. Increase value of I by 1
18. Stop
8/20/2019 Java in BlueJ Project
85/102
COMPUTER SCIENCE ASSIGNMENT
85
85
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;i9)
{
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);
}
}
}
8/20/2019 Java in BlueJ Project
86/102
COMPUTER SCIENCE ASSIGNMENT
86
86
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store the lower limit
n int To store the upper limiti int To run loop
f int To store the nos. of factors of a number
j int To run a loop
r int To extract the digit
a int Dummy variable
s int To store the eventual sum of the digits
c int To count the nos. of composite magic numbers
8/20/2019 Java in BlueJ Project
87/102
COMPUTER SCIENCE ASSIGNMENT
87
87
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
88/102
8/20/2019 Java in BlueJ Project
89/102
COMPUTER SCIENCE ASSIGNMENT
89
89
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=10, then print "SIZE OUT OF RANGE"
Else go to step 44.
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
8/20/2019 Java in BlueJ Project
90/102
COMPUTER SCIENCE ASSIGNMENT
90
9
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
8/20/2019 Java in BlueJ Project
91/102
COMPUTER SCIENCE ASSIGNMENT
91
91
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store the size of the matrix
i int To run a loop j int To run a loop
M[][] int array To store the inputted elements
c int Used as a counter
ls int To store the sum of the left diagonal elements of the matrix
rs int To store the sum of right diagonal elements of the matrix
8/20/2019 Java in BlueJ Project
92/102
8/20/2019 Java in BlueJ Project
93/102
COMPUTER SCIENCE ASSIGNMENT
93
93
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:
Input- M= 100
N=11
Output- The required number= 119
Total number of digits= 3
Example 2:
Input- M= 1500
N= 25
Output- The required number= 1699
Total number of digits= 4
Example 3:
Input- M= 99
N=11
Output- INVALID INPUT
Example:
Input- M= 112
N=130
Output- INVALID INPUT
8/20/2019 Java in BlueJ Project
94/102
COMPUTER SCIENCE ASSIGNMENT
94
94
ALGORITHM
1.
Start
2.
Take M and N as user input
3.
Check if m10000 or n>=100, then print ''INVALID INPUT''
4.
Initialise s=05.
Repeat steps 6 to 12 when s ≠ n
6.
Assign a=m, s=0
7.
Repeat steps 8 to 11 when a>0
8.
Assign r=a%10
9.
Assign a=a/10
10.
Assign s=s+r
11.
Go to step 7
12.
Increase the value of i by 1
13. Assign k=m-1
14. Print k
15.
Stop
8/20/2019 Java in BlueJ Project
95/102
COMPUTER SCIENCE ASSIGNMENT
95
95
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(m10000 || 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());
}
}
}
8/20/2019 Java in BlueJ Project
96/102
COMPUTER SCIENCE ASSIGNMENT
96
96
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store M
n int To store Na int Dummy variable
s int Used in the operation
r int To be used in the extraction of digits of M
8/20/2019 Java in BlueJ Project
97/102
COMPUTER SCIENCE ASSIGNMENT
97
97
OUTPUT IN THE TERMINAL WINDOW
8/20/2019 Java in BlueJ Project
98/102
8/20/2019 Java in BlueJ Project
99/102
COMPUTER SCIENCE ASSIGNMENT
99
99
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=10, then print "SIZE OUT OF RANGE"
Else go to step 44.
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
8/20/2019 Java in BlueJ Project
100/102
COMPUTER SCIENCE ASSIGNMENT
100
1
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=10)
System.out.println("SIZE OUT OF RANGE");
else
{
int M[][]=new int[m][m];
for(i=0;i
8/20/2019 Java in BlueJ Project
101/102
COMPUTER SCIENCE ASSIGNMENT
101
1
DATA VARIABLE DESCRIPTION TABLE
NAME DATA TYPE DESCRIPTION
m int To store the size of the matrix
i int To run a loop j int To run a loop
M[][] int array To store the inputted elements
s int To store the sum of the corner elements
8/20/2019 Java in BlueJ Project
102/102
COMPUTER SCIENCE ASSIGNMENT 1
OUTPUT IN THE TERMINAL WINDOW
Top Related