Professional Documents
Culture Documents
Yagyacomppro
Yagyacomppro
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.
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
2 6 9
5 7 1
12 10 9
3 4
6 12 11 6
5
7 7 8 15
17 18 11 11
Output
Enter the orders of matrix
2 4
2 5 8 11
6 9 8 9
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;
// 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
2 7 6
9 5 1
4 3 8
It is a magic square
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;
}
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
2 5 2 1
9 5 6 1
8 2 7 6
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;
}
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
}
14
Output
Enter the orders of matrix
2 2
1 21
0 2
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 series(int n)
{
System.out.print("Fibonacci Factorial Series: ");
return seriesRecursive(n,0,1);
}
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;
18
if(n%2==0)
return sum-(term/getDenominator(n));
else
return sum+(term/getDenominator(n));
}
}
return denominator;
}
public static void main(String[] args)
{
xyz obj=new xyz();
int n=obj.input();
double sum=obj.seriesSum(n);
19
Output
Enter the value of n
20
{
xyz obj=new xyz();
obj.input();
Output
Enter both the numbers
16 36
HCF of 16 and 36 is 4
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;
}
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
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;
}
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;
}
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;
}
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();
}
}
Output
Enter the number of 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
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
95
48 x 1 = 48
24 x 1 = 24
12 x 1 = 12
6 x 1 = 6
Remaining boxes 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();
char a[]=obj.input(),b[][]=obj.inputAnswers(n);
obj.calc(b,a);
}
}
32
Output
Enter the number of participants
a c d b a
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
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);
}
obj.display(a);
}
}
Output
BIBLIOGRAPHY
I have used the following resources to take information for making
the project:
36