ISC 2022
COMPUTER SCIENCE
PROJECT
U.I.D NO. - 7177895
Name - RUMAYSA
School Name - THE LUCKNOW ...
. . PUBLIC COLLEGIATE
School Code - UP092
Subject Teacher’s Sign. -
Visiting Examiner’s Sign. -
Principal’s Sign. -
School’s Stamp –
QUESTION
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. Allow the user to input
integers into the matrix. Perform the
following tasks:
1.) Display the original matrix.
2.) Rotate the matrix 90 degree
clockwise.
3.) Find the sum of the elements of
the four corners of the matrix.
ALGORITHM
Step1: Create int variables i, j and M.
. Declare an array A[][].
Step2: Take the size of the square
matrix from user and store it in ‘M’.
Step3: If ‘M’ is not within specified
range, show appropriate message.
Step4: Take values in the array.
Step5: Display the array and display
rotation of the array.
__________________________________
__________________________________
import java.io.*;
import java.util.*;
import java.lang.*;
class rotateMatrix
{
int A[][], M, i, j;
Scanner sc=new Scanner(System.in);
void accept()
{
System.out.print(“Enter no. of rows:”);
M=sc.nextInt();
check();
}
void check
{
if (m<=2||m>10)
System.out.println (“Invalid Input.”);
else {
a = new int[M][M];
M--;
for(i=0; i<=M; i++)
{ for(j=0; j<=M; j++)
{ System.out.print(“Enter value:”);
A[i][j]=sc.nextInt();
} }
show();
} }
void show()
{
System.out.println (“The original Matrix:”);
for(i=0; i<=M; i++)
{ for(j=0; j<=M; j++)
{
System.out.print(“ ”+A[i][j]);
}
System.out.println (); }
System.out.println (“The matrix after rotation:”);
for(i=0; i<=M; i++)
{ for(j=0; j<=M; j++)
{
System.out.print(“ ”+A[M-j][i]);
}
System.out.println();
} }
public static void main()
{
rotateMatrix one=new rotateMatrix();
one.accept();
} }
OUTPUT
QUESTION
Write a program to declare a two
dimensional (2-D) Array arr[][]
and store elements in it in rows
and columns, and then sort the
array using BUBBLE SORT
technique.
ALGORITHM
Step1: Start. .
Step2: Create a double dimensional Array.
Step3: Input the size of the array
[r=ROWS][c=COLUMNS].
Step4: Input the values from the user.
Step5: Display the array.
Step6: Create a single dimensional array of
size(r*c).
Step7: Transfer all the values of the single
dimensional array to the double dimensional
array.
Step8: .Repeat ( i loop).
Step9: Repeat (j loop).
Step10: Swap the numbers.
Step11: Convert the single dimensional array
into double dimensional array to print the
result in matrix form.
Step12: End.
import java.util.*;
import java.io.*;
import java.lang.*;
class bsort2d //BUBBLE SORT IN 2D ARRAY
{
public static void main(String args[])
{
int r,c,si,i,j,temp,x=0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter row");
r= sc.nextInt();
System.out.println("Enter column");
c= sc.nextInt();
int arr[][]= new int[r][c];
si=r*c;
int nar[] = new int[si];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++){
System.out.print("Enter values: ");
arr[i][j]= sc.nextInt();
}
}
System.out.print("ARRAY IS:");
System.out.println();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
nar[x++]=arr[i][j];
}
}
for(i=0;i<x;i++)
{
for(j=0;j<x-1;j++)
{
if(nar[j]>nar[j+1])
{
temp=nar[j];
nar[j]=nar[j+1];
nar[j+1]=temp;
}
}
}
System.out.println("NEW OR SORTED ARRAY IS:");
System.out.println();
int a=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[i][j]=nar[a++];
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
OUTPUT
QUESTION
Write a program to input a
sentence and print the NUMBER
OF WORDS in it and show that
how many words starts with
CAPITAL LETTER.
ALGORITHM
Step1: Start. .
Step2: In a constructor initialize the
frequency.
Step3: Take a sentence from user as an input.
Step4: Convert the sentence into an array.
Step5: Check for the white spaces in the
array.
Step6: Then check the next letter of the white
space.
Step7: Insert a white space in the starting.
Step8: At every white space it increases the
count by 1 i.e. each word is counted.
Step9: If the function : “boolean isCap(String
W)” returns true then frequency is increased
by 1 i.e. numder of letters starting with capital
letter.
Step10: 10. Print the result.
Step11: End.
import java.util.*;
import java.io.*;
import java.lang.*;
class StringProg
{
String sent;
int len, freq;
Scanner ob = new Scanner(System.in);
StringProg()
{
freq=0;
}
void input()
{
System.out.println("Enter a sentence");
sent=ob.nextLine();
}
boolean isCap(String W)
{
if(Character.isUpperCase(W.charAt(0))) return true;
else return false;
}
void display()
{
String z[]=sent.split(" ");
len = z.length;
for(int i=0;i<len;i++)
if(isCap(z[i])) freq++;
System.out.println("Sentence is "+ sent);
System.out.println("Frequency of Capital character is "+ freq);
}
public static void main(String arg[])
{
StringProg obj1 = new StringProg();
obj1.input();
obj1.display();
}
}
OUTPUT
QUESTION
Write a program to perform the
following tasks :
1) Input the size of 2 SQUARE
MATRIX.
2) In a new FUNCTION take
input of values of the 2 matrices.
3) In another function DISPLAY
the 2 matrices. 4) Do the
PRODUCT of 2 matrices
according to mathematical rules
and display it. (USE MAIN
FUNCTION ALSO.)
ALGORITHM
Step1: Start. .
Step2: Input size of 2 matrices.
Step3: Take the values of the matrices as a
user input.
Step4: Calculate the product of the 2 matrices
using:
• 1 st row * 1 st column.
• 2 nd row * 2 nd column.
• 2 nd row * 1st column.
• 2 nd row * 2nd column.
(AS PER THE SIZE OF THE MATRIX.)
Step5: Store the value in another matrix.
Step6: Print the matrix.
Step7: End.
import java.util.*;
class MATmultiply
{ int a[][], b[][], c[][];
int n,m;
int i,j,k;
Scanner ob = new Scanner(System.in);
MATmultiply(int mm, int nn)
{ m=mm;
n=nn;
a = new int[m][n];
b = new int[n][m];
c = new int[m][m]; }
void input()
{ System.out.print("Please enter values for first matrices ");
for(i=0;i<m;i++) //rows
for(j=0;j<n;j++) // columns
a[i][j]= ob.nextInt();
System.out.print("Please enter values for second matrices ");
for(i=0;i<n;i++) //rows
for(j=0;j<m;j++) // columns
b[i][j]= ob.nextInt(); }
void display()
{ System.out.println("Values for first matrices");
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("Values for second matrices ");
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
System.out.print(b[i][j]+"\t"); }
System.out.println(); } }
void prod()
{ for(i=0;i<m;i++)
for(j=0;j<m;j++) {
for(k=0;k<n;k++)
c[i][j]= c[i][j] + a[i][k] * b[k][j]; }
System.out.println("Product of both matrices are ");
for(i=0;i<m;i++) {
for(j=0;j<m;j++) {
System.out.print(c[i][j]+"\t"); }
System.out.println(); } }
public static void main(String arg[]) {
MATmultiply obj= new MATmultiply(3,3);
obj.input();
obj.display();
obj.prod();
}
}
OUTPUT
QUESTION
Write a program to input an
array to store numerical
elements and to DELETE a
number according to the user
input.
ALGORITHM
Step1: Start.
Step2: Create a single dimensional array.
Step3: 3.Take values of the array from the
user using Input() function that is called
by the main method.
Step4: Take a value from the user that
needs to be deleted using calc() function
that is called by main method.
Step5: Check the condition: if(a[i]!= n).
Step6: If the condition is true store that
value in an another array(b[]).
Step7: But if the condition is false i.e. the
number is same as the number entered to
be deleted then that number will not be
saved to the new array.
Step8: Then print the remaining element
i.e. the elements of array b[].
Step9: End.
import java.util.*;
class Delete //TO DELETE A NUMBER
FROM AN ARRAY
{
int a[], b[];
int size, n, i, j;
Scanner ob = new Scanner(System.in);
Delete(int s)
{
size = s;
a = new int[size];
b = new int[size];
}
void input()
{
for(i=0;i<size;i++)
{
System.out.println("Enter a number in an array");
a[i] = ob.nextInt();
}
}
void calc()
{
for(i=0;i<size;i++)
System.out.print(a[i] + " ") ;
System.out.println("Enter a number to be deleted
from an array");
n = ob.nextInt();
j=0;
for(i=0;i<size;i++)
if(a[i]!= n) b[j++] = a[i];
System.out.println("The remaining elements in an
array are " );
for(i=0;i<j;i++)
System.out.println(b[i]) ;
}
public static void main(String arg[])
{
Delete obj = new Delete(8);
obj.input();
obj.calc();
}
}
OUTPUT
QUESTION
Write a program to input a
number and find the digits in it
and calculate the
FREQUENCEY of each digit.
ALGORITHM
Step1: Start.
Step2: In a constructor initialize the
variable.
Step3: Take a number as an input from
user.
Step4: Following code extract every digit
of the given number and also calculate the
frequency of any digit : for(i=0;i0) { p =
m% 10; if(p == i) count++; m=m/10; }
Step5: Print the frequency and the digits.
Step6: End.
import java.util.*;
class DigitNumber2
{
int n;
Scanner ob=new Scanner(System.in);
public DigitNumber2()
{
n=0;
}
void input()
{
System.out.println("Enter a number ");
n=ob.nextInt();
}
void find()
{
int p, i,j,count, m;
System.out.println("Digits in the number are ");
for(i=0;i<10;i++)
{
m = n;
count = 0;
while(m>0)
{
p = m% 10;
if(p == i) count++;
m=m/10;
}
if(count!=0) System.out.println(i + " is present at" +
count);
}
}
public static void main(String arg[])
{
DigitNumber2 obj = new DigitNumber2();
obj.input();
obj.find();
}
}
OUTPUT
QUESTION
Write a program to input
number of days and
show the output in a DD
MM YYYY format.
ALGORITHM
Step1: Start.
Step2: Take the number of days as a user
input and the year
Step3: Create an array and store the
number of days in each month as the
elements of an array
Step4: Then check for the leap year and if
it is true then change the value of 2nd
element to 29.
Step5: To convert the number of days to
months and days: while(n>arr[month]) {
n=n-arr[month]; month++; } day=n;
Step6: Print the result in
“DD/MM/YYYY” format.
Step7: End.
import java.util.*;
import java.lang.*;
import java.io.*;
class DateNumber
{
int n, month, year, day;
int i;
Scanner ob = new Scanner(System.in);
DateNumber()
{
month=1;
day=0;
year=0;
}
void input()
{
System.out.println("Enter number of days from 1st January");
n=ob.nextInt();
System.out.println("Enter yyyy");
year=ob.nextInt();
}
void calc()
{
int arr[] =
{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(year%4==0) arr[2]=29;
while(n>arr[month])
{
n=n-arr[month];
month++;
}
day=n;
System.out.println("Date in dd / mm / yyyy format is ");
System.out.println(day + "/" + month + "/" +
year);
}
public static void main(String arg[])
{
DateNumber obj = new DateNumber();
obj.input();
obj.calc();
}
}
OUTPUT
QUESTION
Write a program to input
a number and print all
the PERFECT
NUMBER under it.
ALGORITHM
Step1: Start.
Step2: Take a number as a user input.
Step3: Repeatedly take all the numbers
from 2 to the entered number.
Step4: In another function check the
number for perfect using:
int i, sum =0;
for(i=1;i<x; i++)
if(x%i==0)
sum=sum+i;
return sum;
Step5: Directly print that number as a
perfect number.
Step6: Continue checking for the next
perfect number.
Step7: End.
import java.util.*;
class PerfectNo
{ int lmt;
PerfectNo()
{ lmt=0;
}
void input()
{
Scanner ob = new Scanner(System.in);
System.out.println("Enter the number to check all perfect numbers
below it ");
lmt=ob.nextInt();
}
int sumfactors(int x)
{
int i, sum =0;
for(i=1;i<x;i++)
if(x%i == 0) sum = sum + i;
return sum;
}
void perfectbelow()
{
int n;
for(n = 2; n<=lmt; n++)
{
int p = sumfactors(n);
if(p==n) System.out.println("The number is a perfect number " + n);
} }
public static void main(String arg[])
{
PerfectNo obj = new PerfectNo();
obj.input();
obj.perfectbelow();
}
}
OUTPUT
QUESTION
Write a program to
input a positive
number and check if it
is an EVIL NUMBER.
ALGORITHM
Step1: Start.
Step2: Convert the decimal number to binary
number using:
while(n>0)
{
r=n%2;
s=dig[r]+s;
n=n/2;
}
Step3: Count the number of 1’s in the binary
number.
Step4: If (count%2==0) true means even ,
false means odd.
Step5: If even , print EVIL NUMBER.
Step6: If odd , print NOT AN EVIL
NUMBER.
Step7: End.
import java.util.*;
class EvilNumber
{
String toBinary(int n)
{
int r;
String s="";
char dig[]={'0','1'};
while(n>0)
{
r=n%2;
s=dig[r]+s;
n=n/2;
}
return s;
}
int countOne(String s)
{
int c = 0, l = s.length();
char ch;
for(int i=0; i<l; i++)
{
ch=s.charAt(i);
if(ch=='1')
{
c++;
}
}
return c;
}
public static void main(String args[])
{
EvilNumber ob = new EvilNumber();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a positive number : ");
int n = sc.nextInt();
String bin = ob.toBinary(n);
System.out.println("Binary Equivalent = "+bin);
int x = ob.countOne(bin);
System.out.println("Number of Ones = "+x);
if(x%2==0)
System.out.println(n+" is an Evil Number.");
else
System.out.println(n+" is Not an Evil Number.");
}
}
OUTPUT
QUESTION
Write a program to input
Principal, Rate and Time.
Calculate the Simple Interest and
Compound Interest and also
display the difference between
them using Function arguments.
ALGORITHM
Step1: Start.
Step2: Input principal amount , rate & time.
Step3: Then call function si & pass the
values.
Step4: In function si: out = (p*r*t)/100 &
return out.
Step5: Then call function ci & pass value.
Step6: In function ci: “out” is calculated
&returned.
Step7: Print simple interest.
Step8: Print compound interest.
Step9: Calculate their difference and print.
Step10: End.
import java.util.*;
class intrest
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int princ;
double rate,time ;
System.out.println("Enter Principal ");
princ = sc.nextInt();
System.out.println("Enter Rate ");
rate = sc.nextDouble();
System.out.println("Enter Time in years");
time = sc.nextDouble();
double simple = intrest.si(princ , rate , time);
System.out.println("Simple intrest is " + simple);
double comp = intrest.ci(princ , rate , time);
System.out.println("Compound intrest is " + comp);
System.out.println("Diffrence in simple and compound intrest is " +
(comp-simple));
}
static double si(int p , double r , double t)
{
double out = (p*r*t)/100;
return out;
}
static double ci(int p , double r , double t)
{
double out =p*(Math.pow((1+(r/100)),(12*t)))-p;
return out;
}
}
OUTPUT
QUESTION
The city library charges late fine from
members if the books were not returned
on time as per the following table:
No. of days Magazine Text book
late fine per day fine per day
Up to 5 days 1 Rs. 2 Rs.
6 to 10 days 2 Rs. 3 Rs.
11 to 15 3 Rs. 4 Rs.
days
15 to 20 5 Rs. 6 Rs.
days
More than 6 Rs. 7 Rs.
20 days
Using switch statement, Write a Program
to input name of person, No. of days late
and type of book. Compute and display
the total fine.
ALGORITHM
Step1: Start.
Step2:Input name and days late.
Step3:Input type of book “M” for
Magazine & “T” for text book.
Step4:Using switch case check the type of
book.
Step5:Then check the category i.e. how
much fine per day has to imposed.
Step6:Total fine = fine per day*No. of
days.
Step7:Print the output.
Step8:Else display “invalid book type”.
Step9:End.
import java.util.Scanner;
public class fine
{
public static void main() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
String name = in.nextLine();
System.out.print("Days late: ");
int days = in.nextInt();
System.out.println("Type M for Magazine");
System.out.println("Type T for Text book");
System.out.print("Enter book type: ");
char type = in.next().charAt(0);
int fine = 0;
switch (type) {
case 'M':
if (days <= 5)
fine = 1;
else if (days <= 10)
fine = 2;
else if (days <= 15)
fine = 3;
else if (days <= 20)
fine = 5;
else
fine = 6;
break;
case 'T':
if (days <= 5)
fine = 2;
else if (days <= 10)
fine = 3;
else if (days <= 15)
fine = 4;
else if (days <= 20)
fine = 6;
else
fine = 7;
break;
default:
System.out.println("Invalid book type");
}
int totalFine = fine * days;
System.out.println("Name: " + name);
System.out.println("Total Fine: " + totalFine);
}
}
OUTPUT
QUESTION
Write a program to input a
number from the user and check
whether it is an AMSTRONG
NUMBER or not and print the
output.
ALGORITHM
Step1: Start.
Step2: Take a number as an input from user.
Step3: To calculate the sum of half of the numbers
digit and check the Armstrong following code is used:
while(m>0)
{ c++;
m=m/10;
}
while(n>0)
{ a = n % 10;
sum = sum + (int)Math.pow(a,c);
n= n / 10; }
Step4: If the given number is == to the sum then the
number is an Armstrong number.
Step5: Else it is not an Armstrong number.
Step6: Print the result.
Step7: End.
import java.util.*;
class ArmstrongNo
{
int n,a,sum;
ArmstrongNo()
{
n=0;
sum=0;
}
void input()
{
Scanner ob = new Scanner(System.in);
System.out.println("Enter the number ");
n=ob.nextInt();
}
void show()
{
int p=n;
int m=n;
int c=0;
while(m>0)
{
c++;
m=m/10;
}
while(n>0)
{
a = n % 10;
sum = sum + (int)Math.pow(a,c);
n= n / 10;
}
if(p==sum) System.out.println("Number is Armstrong
Number = " + p);
else System.out.println("Number is NOT a Armstrong
Number = " + p);
}
public static void main(String arg[])
{
ArmstrongNo obj = new ArmstrongNo();
obj.input();
obj.show();
}
}
OUTPUT