You are on page 1of 25

COMPUTER ASSIGNMENT

COMPUTER ASSIGNMENT
TOPIC:PROGRAMMING

ON

JAVA

NAME :-PROJJAL
GOP
CLASS :XI SCIENCE
ROLL NO.:-21
YEAR:-2015-16

COMPUTER ASSIGNMENT

INDEX:Contents

Page No.

1.Program to multiply two matrices.

2.Program to add two matrices.

3.Program to print the sum of right

and left diagonals.


4.Program to find the saddle point.

10

5.Program to check for magic square. 14


6.Program to shift elements.

19

COMPUTER ASSIGNMENT

1. PROGRAM TO MULTIPLY TWO GIVEN


MATRICES.
import java.util.*;
class Matrices
{
int ar[][],col,row;
public Matrices(int r,int c)
{
row=r;
col=c;
ar=new int [row][col];
}
public void accept()
{
int i,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.println("Enter the elemets at the position " + (i+1) + " "
+ (j+1));
ar[i][j]=sc.nextInt();
}
}
}
public void display()
{

COMPUTER ASSIGNMENT

int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
Formatter fmt=new Formatter();
fmt.format("%4d",ar[i][j]);
System.out.print(fmt);
}
System.out.println();
}
}
public void mul(Matrices obj1, Matrices obj2)
{
int i,j,k;
i=j=k=0;
for(i=0;i<obj1.row;i++)
{
for(j=0;j<obj2.col;j++)
{
ar[i][j]=0;
for(k=0;k<obj2.row;k++)
{
ar[i][j]+=obj1.ar[i][k]*obj2.ar[k][j];
}
}
}
}
public static void main()
{

COMPUTER ASSIGNMENT

Scanner sc=new Scanner(System.in);


System.out.println("Enter the row for the first matrix");
int r=sc.nextInt();
System.out.println("Enter the column for the first matrix");
int c=sc.nextInt();
System.out.println("Enter the row for the second matrix");
int r1=sc.nextInt();
System.out.println("Enter the row for the second matrix");
int c1=sc.nextInt();
if(c!=r1)
{
System.out.println("Operation not possible");
System.exit(0);
}
Matrices ob1=new Matrices(r,c);
Matrices ob2=new Matrices(r1,c1);
Matrices ob3=new Matrices(r,c1);
System.out.println("Enter the values for the first matrix");
ob1.accept();
System.out.println("Enter the values for the second matrix");
ob2.accept();
System.out.println("\n First matrix");
ob1.display();
System.out.println("\n Second matrix");
ob2.display();
ob3.mul(ob1,ob2);
System.out.println("Final array after array multiplication");
ob3.display();
}
}

COMPUTER ASSIGNMENT

Output:-

2.ADDITION OF TWO MATRICES.

import java.util.*;
class Addition
{
int ar[][],col,row;

COMPUTER ASSIGNMENT

public Addition(int r,int c)


{
row=r;
col=c;
ar=new int [row][col];
}
public void accept()
{
int i,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.println("Enter the elemets at the position " + (i+1) + " "
+ (j+1));
ar[i][j]=sc.nextInt();
}
}
}
public void display()
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
Formatter fmt=new Formatter();
fmt.format("%4d",ar[i][j]);
System.out.print(fmt);

COMPUTER ASSIGNMENT

}
System.out.println();
}
}
public void add(Addition ob1,Addition ob2)
{
int i,j,k;
i=j=k=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
ar[i][j]=ob1.ar[i][j] + ob2.ar[i][j];
}
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the row ");
int r=sc.nextInt();
System.out.println("Enter the column ");
int c=sc.nextInt();
Addition ob1=new Addition(r,c);
Addition ob2=new Addition(r,c);
Addition ob3=new Addition(r,c);
System.out.println("Enter the values for the first matrix");
ob1.accept();
System.out.println("Enter the values for the second matrix");
ob2.accept();

COMPUTER ASSIGNMENT

System.out.println("\n First matrix");


ob1.display();
System.out.println("\n Second matrix");
ob2.display();
ob3.add(ob1,ob2);
System.out.println("Final array after array addition");
ob3.display();
}
}

Output:-

COMPUTER ASSIGNMENT

3. PROGRAM TO PRINT THE SUM OF THE RIGHT


AND THE LEFT DIAGONALS.
import java.util.*;
class Diagonals
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size of rows and columns");
int r=sc.nextInt();
int c=sc.nextInt();
int ar[][]=new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("Enter the array element at pos " + (i+1) + " " +
(j+1));
ar[i][j]=sc.nextInt();
}
}

COMPUTER ASSIGNMENT

System.out.println("Display array");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(ar[i][j] + " ");
}
System.out.println();
}
int s=0;
for(int i=0;i<r;i++)
{
s=s+ar[i][i];
}
System.out.println("Left diagonal " + s);
int q=0;
for(int i=0;i<r;i++)
{
q=q+ar[i][--c];
}
System.out.println("Right diagonal " + q);
}
}

COMPUTER ASSIGNMENT

Output:-

4.PROGRAM TO FIND THE SADDLE POINT IN THE


ARRAY.
import
java.util.*;
class
Saddle
{
int ar[]
[],n,sd;

COMPUTER ASSIGNMENT

Saddle(int s)
{
n=s;
ar=new int [n][n];
}
public void get()
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("Enter the value of ar[" + (i+1)+ "][" +
(j+1)+"]");
ar[i][j]=sc.nextInt();
}
}
}
void display()
{
for(int i=0;i<n;i++)

COMPUTER ASSIGNMENT

{
for(int j=0;j<n;j++)
System.out.print(ar[i][j]+" ");
System.out.println();
}
}
boolean checkSaddle()
{
for(int i=0;i<n;i++)
{
int pos=i;
int min=ar[i][0];
for(int j=0;j<n;j++)
{
if(min>ar[i][j])
{
min=ar[i][j];
pos=j;
}
}
int max=ar[0][pos];

COMPUTER ASSIGNMENT

for(int j=0;j<n;j++)
if(max<ar[j][pos])
max=ar[j][pos];
if(min==max)
{
sd=max;
return true;
}
}
return false;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter order:");
int d=sc.nextInt();
Saddle obj=new Saddle(d);
obj.get();
obj.display();
if(obj.checkSaddle())
System.out.println("Saddle point is " + obj.sd);

COMPUTER ASSIGNMENT

else
System.out.println("NO SADDLE POINT");
}
}

Output:-

5.PROGRAM TO CHECK FOR THE MAGIC SQUARE


NO.

import java.util.*;
class Magic
{
int ar[][],r,k=0,get;
public void get()
{

COMPUTER ASSIGNMENT

Scanner sc=new Scanner(System.in);


System.out.println("Enter the size of array ");
r=sc.nextInt();
ar=new int[r][r];
get=r;
for(int i=0;i<r;i++)
{
for(int j=0;j<r;j++)
{
System.out.println("Enter the element for row " + (i+1) + " and
column " + (j+1));
ar[i][j]=sc.nextInt();
}
}
for(int i=0;i<1;i++)//Sum of the nos.
{
for(int j=0;j<r;j++)
k+=ar[i][j];
}
}
public boolean check(int a,int b)//Checks for lateral sum

COMPUTER ASSIGNMENT

{
boolean flag=true;
for(int i=0;i<a;i++)
{
int s=0;
for(int j=0;j<b;j++)
{
s+=ar[i][j];
}
if(k!=s)
flag=false;
}
return flag;
}
public boolean check1()//Checks for right diagonal sum
{
int s=0;
for(int i=0;i<r;i++)
{
s+=ar[i][i];

COMPUTER ASSIGNMENT

}
System.out.println(s);
if(k!=s)
return false;
return true;
}
public boolean check2()//Checks for left diagonal sum
{
int s=0;
for(int i=0;i<r;i++)
{
s+=ar[i][--get];
}
System.out.println(s);
if(k!=s)
return false;
return true;
}
public static void main(String args[])
{

COMPUTER ASSIGNMENT

Magic obj=new Magic();


obj.get();
if(obj.check(obj.r,obj.r)!=true)
System.exit(0);
if(obj.check1()!=true)
System.exit(0);
if(obj.check2()!=true)
System.exit(0);
if(obj.check(obj.r,obj.r))
System.out.println("MAGIC");
}
}

Output:-

COMPUTER ASSIGNMENT

6.PROGRAM TO SHIFT ROW ELEMENTS.

COMPUTER ASSIGNMENT

import java.util.*;
class Shift
{
int r,c,ar[][];
public void get()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of row and column");
r=sc.nextInt();
c=sc.nextInt();
ar=new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("Enter the element for row " + (i+1) + " and
column " + (j+1));
ar[i][j]=sc.nextInt();
}
}

COMPUTER ASSIGNMENT

}
public void shift()
{
for(int i=0;i<r/2;i++)
{
for(int j=0;j<c;j++)
{
int tmp=ar[i][j];
ar[i][j]=ar[r-1-i][j];
ar[r-1-i][j]=tmp;
}
}
}
public void display()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(ar[i][j] + " ");
}

COMPUTER ASSIGNMENT

System.out.println();
}
}
public static void main()
{
Shift obj=new Shift();
obj.get();
System.out.println("Original array");
obj.display();
System.out.println("New array");
obj.shift();
obj.display();
}
}

COMPUTER ASSIGNMENT

Output:-

You might also like