Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 36

Index

Serial Topic Page No.


Number
1. Acknowledgement 3
2.
3.
4.
5.
6.
7.
8.
9.

1
Acknowledgement
2
I would like to express my sincere gratitude to all those who have contributed
to the successful completion of my computer project. Firstly, I am deeply
thankful to my computer science teacher, Mr./Ms. [Teacher's Name], for their
invaluable guidance, mentorship, and continuous support. Their expertise and
dedication have been instrumental in shaping my understanding of the subject.
Furthermore, I am grateful to my friends and classmates for their collaborative
efforts, insightful discussions, and constructive feedback, which have
significantly enriched my project.

Lastly, I acknowledge the contributions of the research scholars, authors, and


developers whose work has served as a valuable source of information and
inspiration for my project.

In conclusion, I am thankful to all those who have played a part, directly or


indirectly, in making this project a reality. Your collective support,
encouragement, and guidance have been invaluable, and I am truly grateful for
the opportunities provided.

Yagyapreet Singh

3
1Write a java program to perform matrix multiplication of two 2D array of
same size

import java.util.*;
class xyz
{
int[][] input(int x)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the orders of matrix no."+x);
int a[][],c=in.nextInt(),d=in.nextInt(); // inputs the rows and columns of
the matrix number 'x'
a=new int[c][d];
System.out.println("Enter the elements of the matrix");
for(int i=0;i<c;i++)
{
for(int j=0;j<d;j++)
a[i][j]=in.nextInt(); // inputs the matrix number 'x'
}
return a;
}
int[][] multiply(int m1[][], int m2[][]) // finds the multiplication of
the 2 inputed matrixes
{
int result[][] = new int[m1.length][m2[0].length];
for (int i = 0; i < m1.length; i++)
{
for (int j = 0; j < m2[0].length; j++)
{
result[i][j]=0;
for (int k = 0; k < m1[0].length; k++)

4
result[i][j] += m1[i][k]*m2[k][j];
}
}
return result;
}
public static void main(String[] args)
{
xyz obj=new xyz();
int a[][]=obj.input(1),b[][]=obj.input(2),r[][];
r=obj.multiply(a, b); // stores the multiplied matrix returned by function
'multiply'
System.out.println("The matrix after multiplication is");
for(int i=0;i<r.length;i++)
{
for(int j=0;j<r[0].length;j++)
System.out.print(r[i][j]+" ");
System.out.println();
}
}
}

Output
Enter the orders of matrix no.1

3 3

Enter the elements of the matrix

2 6 9

5 7 1

12 10 9

Enter the orders of matrix no.2

3 4

Enter the elements of the matrix

6 12 11 6

5
7 7 8 15

17 18 11 11

The matrix after multiplication is

207 228 169 201

96 127 122 146

295 376 311 321

2 Write a Java program to calculate the transpose of a 2D array.


import java.util.*;
class xyz
{
int[][] input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the orders of matrix");
int a[][],c=in.nextInt(),d=in.nextInt(); // inputs the rows and columns of
the matrix
a=new int[c][d];
System.out.println("Enter the elements of the matrix");
for(int i=0;i<c;i++)
{
for(int j=0;j<d;j++)
a[i][j]=in.nextInt(); // inputs the matrix
}
return a;
}

public static void main(String[] args)


{
xyz obj=new xyz();
int a[][]=obj.input(),t[][];
6
t= new int[a[0].length][a.length];
for (int i=0;i<a.length;i++)
{
for (int j=0;j<a[0].length;j++)
{
t[j][i]=a[i][j]; //interchanges the row with column, i.e finds
transpose of a
}
}

System.out.println("Transpose of the array:");


for(int i=0;i<t.length;i++)
{
for(int j=0;j<t[0].length;j++)
System.out.print(t[i][j]+" "); // prints the transpose matrix
System.out.println();
}
}
}

Output
Enter the orders of matrix

2 4

Enter the elements of the matrix

2 5 8 11

6 9 8 9

Transpose of the array:

2 6

5 9

8 8

11 9
7
3 Write a Java program to check if a given 2D array is a magic square. A magic square is a
square matrix in which the sum of each row, each column, and each diagonal is the same.
import java.util.*;
class xyz
{
int[][] input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the order of matrix");
int a[][],c=in.nextInt(); // inputs the order of the matrix
a=new int[c][c];
System.out.println("Enter the elements of the matrix");
for(int i=0;i<c;i++)
{
for(int j=0;j<c;j++)
a[i][j]=in.nextInt(); // inputs the matrix
}
return a;
}
boolean magic(int a[][])
{
int r1=0,r2=0,c=0,d1=0,d2=0,i,j;

// Calculate the sum of the first row


for(i=0;i<a[0].length;i++)
r1+=a[0][i];

// Check rows

8
for(i=0;i<a.length;i++)
{
r2=0;
for(j=0;j<a[0].length;j++)
r2+=a[i][j];
}
if(r1!=r2)
return false;
// Check columns
for(j=0; j<a.length;j++)
{
c=0;
for(i=0;i<a[0].length;i++)
c+=a[i][j];
}
if(c!=r1)
return false;
// Check main diagonal
for(i=0;i<a.length;i++)
d1+=a[i][i];
if(d1!=r1)
return false;
// Check anti-diagonal
for(i=0;i<a.length;i++)
d2+=a[i][a.length-i-1];
if(d2!=r1)
return false;
return true;
}
public static void main(String[] args)
{

9
xyz obj=new xyz();
int a[][]=obj.input();
if(!obj.magic(a
System.out.println("Not a magic square");
else
System.out.println("It is a magic square");
}
}

Output
Enter the order of matrix

Enter the elements of the matrix

2 7 6

9 5 1

4 3 8

It is a magic square

4 Rotate a matrix by 90 degrees clockwise.


import java.util.*;
class xyz
{
int[][] input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the orders of matrix");

10
int a[][],c=in.nextInt(),d=in.nextInt(); // inputs the orders of the
matrix
a=new int[c][d];
System.out.println("Enter the elements of the matrix");
for(int i=0;i<c;i++)
{
for(int j=0;j<d;j++)
a[i][j]=in.nextInt(); // inputs the matrix
}
return a;
}
int[][] rotate(int a[][])
{
int b[][]=new int[a[0].length][a.length];
for(int i=0;i<a[0].length;i++)
{
for(int j=0;j<a.length;j++)
{
b[i][j]=a[a.length-j-1][i]; //interchanges the element accordingly
}
}
return b;
}

public static void main(String[] args)


{
xyz obj=new xyz();
int a[][]=obj.input(),b[][];
b=obj.rotate(a);
System.out.println("Matrix after rotating by 90 degree is");
for(int i=0;i<b.length;i++)

11
{
for(int j=0;j<b[0].length;j++)
System.out.print(b[i][j]+" "); // prints the rotated matrix

System.out.println();
}
}
}

Output
Enter the orders of matrix

3 4

Enter the elements of the matrix

2 5 2 1

9 5 6 1

8 2 7 6

Matrix after rotating by 90 degree is

8 9 2

2 5 5

7 6 2

6 1 1

5 Find the saddle point(s) in a 2D array. Question: Write a Java program to find the saddle
point(s) in a given 2D array. A saddle point is an element in the matrix that is both the
smallest in its row and the largest in its column.

import java.util.*;
class xyz
{
int[][] input()
12
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the orders of matrix");
int a[][],c=in.nextInt(),d=in.nextInt(); // inputs the orders of the
matrix
a=new int[c][d];
System.out.println("Enter the elements of the matrix");
for(int i=0;i<c;i++)
{
for(int j=0;j<d;j++)
a[i][j]=in.nextInt(); // inputs the matrix
}
return a;
}

void saddle(int a[][])


{
boolean hasSaddle = false;
for(int i=0;i<a.length;i++)
{
int minRow=a[i][0];
int colIndex=0;
// Find the minimum value and its column index in the current row
for(int j=1;j<a[0].length;j++)
{
if(a[i][j]<minRow)
{
minRow=a[i][j];
colIndex=j;
}
}

13
// Check if the minimum value is the maximum in its column
boolean isSaddle=true;
for(int k=0;k<a.length;k++)
{
if(a[k][colIndex]>minRow)
{
isSaddle=false;
break;
}
}
if(isSaddle)
{
System.out.println("Saddle point found at (" + i + ", " + colIndex + ")");
// Print the coordinates of the saddle point if found
hasSaddle=true;
}
}
if(!hasSaddle)
System.out.println("No saddle points found."); // If no saddle point is
found, print a message
}

public static void main(String[] args)


{
xyz obj=new xyz();
int a[][]=obj.input(),b[][];
obj.saddle(a);
}
}

14
Output
Enter the orders of matrix

2 2

Enter the elements of the matrix

1 21

0 2

Saddle point found at (0, 0)

1 Calculate and find the sum of the Fibonaci Factorial series


0*0!+1*1!+1*1!+2*2!+3*3!+5*5*- - - - n times

import java.util.*;
class xyz
{
int input()
{
Scanner in=new Scanner(System.in);
15
System.out.println("Enter the value of n");
int n=in.nextInt(); // inputs the number up to which the sum of series will be
calculated
return n;
}

long factorial(long n) // calculates the factorial of the parameter


{
if(n==0)
return 1;
else
return n*factorial(n-1);
}

long seriesRecursive(int n,long first,long second)


{
if(first==0)
System.out.print(0+" ");
if(n==0)
return 0;
else if(n==1)
return 0;
else
{
long next=second*factorial(second);
System.out.print(next+" "); // prints the series
16
return next+seriesRecursive(n-1,second,first+second); // recalls the
function
}
}

long series(int n)
{
System.out.print("Fibonacci Factorial Series: ");
return seriesRecursive(n,0,1);
}

public static void main(String[] args)


{
xyz obj=new xyz();
int n=obj.input();
System.out.println("The sum of the series is "+obj.series(n));
}
}

Output
Enter the value of n

Fibonacci Factorial Series: 0 1 1 4 18 600 322560 The sum of the series is 323184

17
2 Write a program to display the sum of the given series using recursion
1−(1∗2)/(1+2)+(1∗2∗3)/(1+2+3)−(1∗2∗3∗4)/(1+2+3+4+ ---- n times

import java.util.*;
class xyz
{
int input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=in.nextInt(); // inputs the number up to which the sum of series
will be calculated
return n;
}
double seriesSum(int n)
{
if(n==1)
return 1.0;
else
{
double sum=seriesSum(n-1);
double term=1.0;

for(int i=1; i<=n;i++) // calculates factorial


term *= i;

18
if(n%2==0)
return sum-(term/getDenominator(n));
else
return sum+(term/getDenominator(n));
}
}

double getDenominator(int n) //calculates the denominator of the series


{
double denominator=0.0;

for (int i=1;i<=n;i++)


denominator += i;

return denominator;
}
public static void main(String[] args)
{
xyz obj=new xyz();
int n=obj.input();
double sum=obj.seriesSum(n);

System.out.println("Sum of the series for n = "+n+": "+sum);


}
}

19
Output
Enter the value of n

Sum of the series for n = 4: -1.0666666666666664

3 find hcf using recursion


import java.util.*;
class xyz
{
static int a,b;
void input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter both the numbers");
a=in.nextInt();
b=in.nextInt(); // inputs the numbers whose HCF is to be calculated
}

int HCF(int num1,int num2) // recursive function to find the HCF


{
if (num2!=0)
return HCF(num2,num1%num2);
else
return num1;
}
public static void main(String[] args)

20
{
xyz obj=new xyz();
obj.input();

System.out.println("HCF of "+a+" and "+b+" is "+obj.HCF(a, b));


}
}

Output
Enter both the numbers

16 36

HCF of 16 and 36 is 4

4 palindrome check using recursion


import java.util.*;
class xyz
{

String input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the string");
String a=in.next(); // inputs the String which needs to be checked
return a;
}

boolean isPalindrome(String a) // checks whether the String a is


palindrome or not
21
{
if(a.length()<=1)
return true;
else if(a.charAt(0)!=a.charAt(a.length()-1))
return false;
else
return isPalindrome(a.substring(1,a.length()-1));
}
public static void main(String[] args)
{
xyz obj=new xyz();
String a=obj.input();

if(obj.isPalindrome(a))
System.out.println(a+" is Palindrome");
else
System.out.println(a+" is not Palindrome");

}
}

Output
Enter the string

PleaseTakeItEasysaEtIekaTesaelP

PleaseTakeItEasysaEtIekaTesaelP is Palindrome

5 neon check using recursion


import java.util.*;
22
class xyz
{
int input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number");
int a=in.nextInt(); // inputs the numbers who needs to be checked
return a;
}

boolean isNeon(int square,int sum,int originalNumber) // checks whether


'a' is neon or not
{
if(square==0)
return (sum==originalNumber);
return isNeon(square/10,sum+(square%10),originalNumber);
}

public static void main(String[] args)


{
xyz obj=new xyz();
int a=obj.input();
if(obj.isNeon(a*a, 0, a))
System.out.println(a+" is a Neon Number");
else
System.out.println(a+" is not a Neon Number");
}
}

23
Output
Enter the number

9 is a Neon Number

1 goldbatcg no
import java.util.*;
class xyz
{
int input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number");
int a=in.nextInt(); // inputs the numbers who needs to be checked
return a;
}

boolean isPrime(int n) // checks if n is prime


{
for(int i=2;i<n;i++)
{
if (n%i==0)
return false;
}

24
return true;
}
boolean isGoldbach(int n) // checks if n is goldbatch no.
{
for(int i=2;i<=n/2;i++)
{
if (isPrime(i)&&isPrime(n-i)) // checks the condition of goldbatch, i.e
the sum of 2 prime factors
return true;
}
return false;
}

public static void main(String[] args)


{
xyz obj=new xyz();
int a=obj.input();
if (a%2==0&&obj.isGoldbach(a))
System.out.println(a + " is a Goldbach number.");
else
System.out.println(a + " is not a Goldbach number.");
}
}

Output
Enter the number

20

20 is a Goldbach number.
25
2 The names of the teams participating in a competition should be displayed on a banner
vertically, to accommodate as many teams as possible in a single banner. Design a program
to accept the names of N teams, where 2 < N< 9 and display them in vertical order, side by
side with a horizontal tab (i.e. eight spaces)

import java.util.*;
class xyz
{
String[] input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of teams");
int b= in.nextInt();
in.nextLine(); // Consume the newline character
String[] a=new String[b];
System.out.println("Enter the names of the teams");
for(int i=0;i<b;i++)
a[i]=in.nextLine();
return a;
}

void display(String a[])


{
int max=0;
for(int i=0;i<a.length;i++)
{
if(a[i].length()>max)
max=a[i].length(); //finds the string with maximum length to take it as a
reference later

26
}
for(int i=0;i<max;i++)
{
for(int j=0;j<a.length;j++)
{
if(i<a[j].length())
System.out.print(a[j].charAt(i)+"\t"); //prints the i'th character of the
string if it still has more letters
else
System.out.print(" \t");
}
System.out.println();
}
}

public static void main(String[] args)


{
xyz obj=new xyz();
String a[]=obj.input();
obj.display(a);
}
}

Output
Enter the number of teams

Enter the names of the teams

Rust

Iron

Amber

Ark
27
Blocks

R I A A B

u r m r l

s o b k o

t n e c

r k

3 A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6


boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to
be packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the
cartons used in descending order of capacity (i.e. preference should be given to the highest
capacity available, and if boxes left are less than 6, an extra carton of capacity 6 should be
used.)

import java.util.*;
class xyz
{
int input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of boxes to be packed (maximum up to
1000 boxes):");
int a=in.nextInt();
return a;
}

void display(int n)
{
if (n>1000)
{
System.out.println("Invalid input"); //to now allow more than 1000 boxes
28
return;
}
int cartons[]={48,24,12,6};
int count[]=new int[4];
int remaining=n;
for(int i=0;i<4;i++)
{
count[i]=remaining/cartons[i]; // to store no.of cartons used of that
particular size
remaining%=cartons[i]; // to find remaining no. of cartons
}
System.out.println("Break-up of the cartons");
for(int i=0;i<4;i++)
{
if(count[i]>0)
System.out.println(cartons[i] + " x " + count[i] + " = " + cartons[i] *
count[i]); //printing as needed
}
System.out.println("Remaining boxes "+remaining);
System.out.println("Total number of boxes "+n);
if(remaining>0)
count[3]++;
System.out.println("Total number of cartons " + (count[0] + count[1] +
count[2] + count[3]));
}
public static void main(String[] args)
{
xyz obj=new xyz();
int a=obj.input();
obj.display(a);
}
}

29
Output

Enter the number of boxes to be packed (maximum up to 1000 boxes):

95

Break-up of the cartons

48 x 1 = 48

24 x 1 = 24

12 x 1 = 12

6 x 1 = 6

Remaining boxes 5

Total number of boxes 95

Total number of cartons 5

4 The quiz has five questions with four multiple choices (A, B, C, D), with each question
carrying 1 mark for the correct answer. Design a program to accept the number of
participants N such that N must be greater than 3 and less than 11. Create a double
dimensional array of size (Nx5) to store the answers of each participant row-wise. Calculate
the marks for each participant by matching the correct answer stored in a single
dimensional array of size 5. Display the scores for each participant and also the
participant(s) having the highest score.

import java.util.*;
class xyz
{
static Scanner in=new Scanner(System.in);
char[] input()
30
{
System.out.println("Enter the answer key separated by space");
char a[]=new char[5];
for(int i=0;i<5;i++)
a[i]=Character.toUpperCase(in.next().charAt(0)); // inputs the answer key
return a;
}
char[][] inputAnswers(int n)
{
char answers[][]=new char[n][5];
System.out.println("Enter the answers for each participant");
for(int i=0;i<n;i++)
{
System.out.println("Participant "+(i+1)+"=");
for (int j=0;j<5;j++)
answers[i][j]=Character.toUpperCase(in.next().charAt(0)); // inputs
answers for each participant separately
}
return answers;
}
void calc(char answers[][],char key[])
{
int[] scores=new int[answers.length];
for(int i=0;i<answers.length;i++)
{
for(int j=0;j<5;j++)
{
if(answers[i][j]==key[j])
scores[i]++; //calculates the Score
}
}

31
System.out.println("Scores=");
for (int i=0;i<scores.length;i++)
System.out.println("Participant "+(i+1)+" = "+scores[i]);
int maxScore=scores[0];
for(int i=0;i<scores.length;i++)
{
if(scores[i]>maxScore)
{
maxScore=scores[i]; // finds highest score
}
}
for(int i=0;i<answers.length;i++)
{
if(scores[i]==maxScore)
System.out.println("Highest Score: Participant(s) "+(i+1)); // checks for
more than one highest scorers
}
}
public static void main(String[] args)
{
xyz obj=new xyz();

System.out.println("Enter the number of participants");


int n=in.nextInt();

char a[]=obj.input(),b[][]=obj.inputAnswers(n);
obj.calc(b,a);
}
}

32
Output
Enter the number of participants

Enter the answer key separated by space

a c d b a

Enter the answers for each participant

Participant 1=

a c b b a

Participant 2=

a b d b a

Participant 3=

a c b a a

Participant 4=

b c b b a

Scores=

Participant 1 = 4

Participant 2 = 4

Participant 3 = 3

Participant 4 = 3

Highest Score: Participant(s) 1

Highest Score: Participant(s) 2

33
5

import java.util.*;
class xyz
{
String input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the sentence");
String a=in.nextLine();
return a;
}

void display(String a)
{

34
String b="";
for(int i=0;i<a.length();i++)
{
if(Character.isLetter(a.charAt(i))) //checks if character is a letter, if
yes then only cipher it
{
if(Character.isLetter((char)(a.charAt(i)+13)))
b+=(char)(a.charAt(i)+13); // if character still lands in alphabetics then
directly store it
else
b+=(char)(a.charAt(i)+13-26); // convert character into alphabet
}
else
b+=a.charAt(i);
}
System.out.println("The cipher text is:\n"+b);
}

public static void main(String[] args)


{
xyz obj=new xyz();
String a=obj.input();

obj.display(a);

}
}

Output

Enter the sentence


35
hello? heLLO? (voice echoing) Is anyone there?

The cipher text is:

uryyb? urYYB? (ibvpr rpubvat) Vf nalbar gurer?

BIBLIOGRAPHY
I have used the following resources to take information for making
the project:

i. ISC class 11th and 12th textbook


ii. https://www.aplustopper.com
iii. Special acknowledgment goes to OpenAI for providing access to the GPT-3 language
model, which assisted in syntaxes and providing guidance

36

You might also like