0% found this document useful (0 votes)
28 views

Xii Assignment Isc

The document contains code for sorting, swapping, scalar, multiplication, circular, rotating, mirroring, and saddle point matrices in Java. It includes code to input matrix elements, perform the specified operation on the matrix, and output the results. For each operation there is sample code, input, and output provided as an example. The code demonstrates various matrix operations and algorithms in Java.

Uploaded by

Jeyasudha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Xii Assignment Isc

The document contains code for sorting, swapping, scalar, multiplication, circular, rotating, mirroring, and saddle point matrices in Java. It includes code to input matrix elements, perform the specified operation on the matrix, and output the results. For each operation there is sample code, input, and output provided as an example. The code demonstrates various matrix operations and algorithms in Java.

Uploaded by

Jeyasudha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

EX: 1 SORTING

Coding:

import java.util.*;
class sorting
{
public static void main()
{
int m[][]=new int[4][4];
int i,j,k,t;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements in the array:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=sc.nextInt();
}
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
{
if(m[i][k]>m[i][k+1])
{
t=m[i][k];
m[i][k]=m[i][k+1];
m[i][k+1]=t;
}
}
}
}
System.out.println("the arranged elements are:");
for(i=0;i<4;i++)
{
for (j=0; j<4;j++)

{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Ex. 2 SWAPPING

Coding:

import java.util.*;
class swap
{
public static void main()
{
int i,j,m,n;
Scanner in=new Scanner(System.in);
System.out.println("Enter no.of rows and columns of array:");
m=in.nextInt();
n=in.nextInt();
int a[][]=new int[m][n];
System.out.println("Enter elements of an array:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
int b[][]=new int[m][n];
for(j=0;j<n;j++)
{
int c=j+1;
if(c>n-1)
c=0;
for(i=0;i<m;i++)
{
b[i][c]=a[i][j];
}
}
System.out.println("Array after swap:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
Output:
EX :3 SCALAR MATRIX

Coding :
import java.util.*;
class scalar
{
public static void main(String args[])
{
int i,j,c=0,d=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows and columns");
int n=in.nextInt();
System.out.println("Enter the elements");
int a[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<(n-1);i++)
{
if(a[i][i]==a[i+1][i+1])
c++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
if(a[i][j]==0)
d++;
}
}
}
if(c==(n-1)&&d==(n*n)-n)
{
System.out.println("It is a scalar matrix");
}
else
{
System.out.println("It is a scalar matrix");
}
}
}
Output :
EX : 4 MULTIPLICATION OF MATRIX

Coding:
import java.util.*;
class matrixmultiplication
{
public static void main(String args[])
{
int i,j,k;
int m[][]=new int[4][4];
int n[][]=new int[4][4];
int p[][]=new int[4][4];
Scanner in=new Scanner(System.in);
System.out.println("Enter 1 st matrix");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=in.nextInt();
}
}
System.out.println(" 1 st matrix :");
for(i=0;i<4; i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+"\t");
}
System.out.println();
}

System.out.println("Enter 2 nd matrix");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
n[i][j]=in.nextInt();
}
}
System.out.println("2 nd matrix :");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(n[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
p[i][j]=0;
for(k=0;k<4;k++)
{
p[i][j]=p[i][j]+m[i][k]*n[k][j];
}
}
}
System.out.println("The product of two matrix :");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(p[i][j]+"\t");
}
System.out.println();
}
}
}

Output :
EX : 5 CIRCULAR MATRIX

Coding :
import java.util.*;
class circular_matrix
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,r1,r2,c1,c2,k=1,i,j;
System.out.println("Enter the size of the matrix");
n=in.nextInt();
int a[][]=new int[n][n];
c1=0;c2=n-1;r1=0;r2=n-1;
do
{
for(i=c1;i<=c2;i++)
{
a[r1][i]=k;
k++;
}
for(j=r1+1;j<=r2;j++)
{
a[j][c2]=k;
k++;
}
for(i=c2-1;i>=c1;i--)
{
a[r2][i]=k;
k++;
}
for(j=r2-1;j>=r1+1;j--)
{
a[j][c1]=k;
k++;
}
c1++;c2--;r1++;r2--;
}
while(k<=n*n);
System.out.println("Circular matrix is listed below :");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
Output:
EX : 6 ROTATING MATRIX

Coding:
import java.util.*;
class rotmx
{
public static void main(String args[])
{
int m,i,j,s=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the matrix dimension");
m=in.nextInt();
if(m>2&&m<10)
{
int a[][]=new int[m][m];
System.out.println("Enter matrix elements");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
a[i][j]=in.nextInt();
}
System.out.println("Original matrix");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println("Matrix after rotation");
for(i=0;i<m;i++)
{
for(j=m-1;j>=0;j--)
{
if(i==0&&j==0||i==0&&j==m-1||i==m-1&&j==0||i==m-1&&j==m-1)
s=s+a[i][j];
System.out.print(a[j][i]+"\t");
}
System.out.println();
}
System.out.println("Sum of the corner elements="+s);
}
else
System.out.println("Size out of range");
}
}

Output:
Ex :7 MIRROR MATRIX
Coding:
import java.util.*;
class mirror_matrix
{
public static void main()
{
int i,j,t=0,n,m,k,t1=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter m,n");
m=sc.nextInt();
n=sc.nextInt();
int b[][]=new int[m][n];
System.out.println("enter the elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("The mirror matrix is:");
if(n%2==0)
{
for(i=0;i<m;i++)
{
t=n-1;
for(j=0;j<=((n/2)-1);j++)
{
t1=b[i][j];
b[i][j]=b[i][j+t];
b[i][j+t]=t1;
t-=2;
}
}
}
else
{
for(i=0;i<m;i++)
{
k=n-1;
for(j=0;j<=((n/2)-1);j++)
{
t1=b[i][j];
b[i][j]=b[i][j+k];
b[i][j+k]=t1;
k-=1;
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Ex :8 SADDLE POINT
Coding:
import java.io.*;
import java.util.*;
class saddlepoint
{
public static void main()
{
int c=0,c1=0,ptr=0,f=0;
int temp;
Scanner ss = new Scanner(System.in);
System.out.println("enter n");
int n=ss.nextInt();
int a[][]=new int[n][n];
int n1;
int i,j,k,s;
System.out.println("Enter array elements");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=ss.nextInt();
}

for(i=0;i<n;i++)
{ k=0;
for(s=0;s<n;s++)
{
n1=a[i][k];
for(j=0;j<n;j++)
{
if(n1<a[i][j])
{
c++;
}
if(n1>a[j][s])
{
c1++;
}
}
if(c==(n-1)&&c1==(n-1))
{
f=1;
System.out.println(n1+"is the saddle point");
}
k++;

}
}
if(f==0)
System.out.println("No saddle point");
System.out.println("The original array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int g=n+1;
int b[]=new int[g];
b[0]=-999;
g=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
b[g]=a[i][j];
g++;
}
}
}
for(i=1;i<=n;i++)
{
temp=b[i];
ptr=i-1;
while(b[ptr]>temp)
{
b[ptr+1]=b[ptr];
ptr--;
}
b[ptr+1]=temp;
}
System.out.println("The matrix after sorting");
g=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
System.out.print(a[i][j]+" ");
}
if(i==j)
{
System.out.print(b[g]+" ");
g++;
}
}
System.out.println();
}
}
}

Output:
Ex. : 9 WONDROUS SQUARE
Coding:

import java.util.*;
class wonder
{
public static void main()
{
int n,d=0,i,j,s1,s2,c=0;
double t;
Scanner in=new Scanner(System.in);
System.out.println("Enter n");
n=in.nextInt();
int a[][]=new int[n][n];
System.out.println("enter the elements of matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=in.nextInt();
}
t=(0.5)*n*((n*n)+1);
for(i=0;i<n;i++)
{
s1=0;s2=0;
for(j=0;j<n;j++)
{
s1=s1+a[i][j];

s2=s2+a[j][i];
}
if(s1==t&&s2==t)
c++;
}
if(c==n)
System.out.println("wondrous square");
else
System.out.println("not a wondrous square");
System.out.println("prime \t"+"Row index\t"+"Column index");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
d=0;
for(int k=2;k<=a[i][j];k++)
{
if(a[i][j]%k==0)
d++;
}
if(d==1)
System.out.println(a[i][j]+"\t\t"+i+"\t\t"+j);
}
}
}
}
Output :
Ex.:10 Sum of boundary elements
Coding:
import java.io.*;
import java.util.*;
class sum
{

void Boundary(int a[][],int n)

int s=0;

int i,j;

int c=0,r=0,c1,r1;

c1=n-1;r1=n-1;

for(i=c;i<=c1;i++)

s=s+a[r][i];

for(j=r+1;j<=r1;j++)

s=s+a[j][c1];

for(i=c1-1;i>=c;i--)

s=s+a[r1][i];

for(j=r1-1;j>=r+1;j--)

s=s+a[j][c];

System.out.println("Sum of boundary elements:"+s);

public static void main()

Scanner in=new Scanner(System.in);

int i,j;
System.out.println("Enter dimension of the array:");

int m=in.nextInt();

int b[][]=new int[m][m];

System.out.println("Enter elements of an array:");

for(i=0;i<m;i++)

for(j=0;j<m;j++)

b[i][j]=in.nextInt();

sum ob=new sum();

ob.Boundary(b,m);

}
Output:

Ex: 11 MAGIC NUMBER


Coding:
import java.util.*;
class magic
{
public static void main()
{
int n,d=0,c,s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
n=sc.nextInt();
s=n;
while(s>9)
{
n=s;s=0;
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;
}
}
if(s==1)
{
System.out.println("Magic Number");
}
else
{
System.out.println("Not a Magic Number");
}
}
}
Output:

Ex No: 12 CIRCULAR PRIME


Coding:-
import java.util.*;

class CircularPrime

public static void main(String args[])

Scanner in=new Scanner(System.in);

int i,j,n,p,r1=0,r2=0,c=0,f=0;

System.out.println("enter a number");

n=in.nextInt();

System.out.println("the different combinations of prime numbers are");

p=n;c=0;

while(p>0)

p=p/10;

c++;

p=n;

Outer:

for(i=1;i<=c;i++)

f=1;

for(j=2;j<p;j++)

if(p%j==0)

f=0;

break Outer;

if(f==1)
{

System.out.println(p);

r1=(int)(p/Math.pow(10,c-1));

r2=(int)(p%Math.pow(10,c-1));

p=r2*10+r1;

if(f==0)

System.out.println("not a circular prime");

else

System.out.println("hence,"+n+" is a circular prime");

Output:

EX : 13 SMITH NUMBER
Coding:
import java.io.*;
import java.util.*;
class smith_number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int t=n;
int re=0,r=0,sum=0,sum1=0,d=0,i=2,j=1,c=0;
while(n>0)
{
re=n%10;
sum+=re;
n/=10;
}
n=t;
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
if(n%i==0)
{
while(n%i==0)
{
n/=i;
d=i;
while(d>0)
{
r=d%10;
sum1+=r;
d/=10;
}
}
}
}
c=0;
d=0;
}
if(sum1==sum)
{
System.out.println(t+" is a Smith Number");
}
else
{
System.out.println(t+" is not a smith number");
}
}
}

Output:
Ex. No.14 Composite Magic Number
Coding:
import java.util.*;
class composite_magic
{
public static void main()
{
int m,n,i,c,f,t,a,j;
f=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter range:");
m=in.nextInt();
n=in.nextInt();
if(m>n)
System.out.println("Invalid range");
else
{
System.out.println("Composite magic numbers:");
for(i=m;i<=n;i++)
{
int k=i;
c=0;
for(j=2;j<=k;j++)
{
if(k%j==0)
c++;
}
if(c>2)
{
int l=i;
t=l;
while(t>9)
{
l=t;t=0;
while(l>0)
{
a=l%10;
t=t+a;
l=l/10;
}
}
if(t==1)
{
System.out.println(i);
f++;
}
}
}
System.out.println("Frequency of the composite magic numbers:"+f);
}
}
}

Output:
EX : 15 EVIL NUMBER
Coding :
import java.io.*;

class evil

public static void main(int n)

int c=0,r=0;

String s="";

while(n>0)

r=n%2;

if(r==1)

c++;

n/=2;

if(c%2==0)

System.out.println("Evil number");

else

System.out.println("Not");

Output :
Ex.No:16 BOUNCY NUMBER
Coding:
import java.io.*;
class Bouncy_Number
{
public static void main(int n)
{
int i,c=0,t=n,d=0,x;
while(n!=0)
{
n/=10;
c++;
}
n=t;
int a[]=new int[c];
for(i=0;i<c;i++)
{
a[i]=n%10;
n/=10;
}
n=t;x=c;c=0;
for(i=1;i<x;i++)
{
if(a[i-1]>=a[i])
c++;
if(a[i-1]<=a[i])
d++;
}
if(c==(x-1)||d==(x-1)||n<100)
System.out.println(n+" is not a bouncy number");
else
System.out.println(n+" Is a bouncy number");
}
}

Output:
EX NO:17 PRONIC NUMBER

CODING:-

import java.io.*;
import java.util.*;
class pronic
{
public static void main()
{
Scanner sc=new Scanner(System.in);

int i,h=0,n;
System.out.println("Enter the number to check:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i*(i+1)==n)
h=1;
}
if(h==1)
System.out.println("Pronic number");
else
System.out.println("Not");
}
}

Output:-
Ex no: 18 Fascinating Numbers
Coding :
import java.util.*;
class fascinating
{
public static void main()
{
int n,n1,n2,j,f=1;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number:");
n=in.nextInt();
if(n<100)
{
System.out.println(n + "is not fascinating number");
}
n1=n*2;
n2=n*3;
int b=0;
String v=n + " " + n1 +" " +n2;
System.out.println(v);
int x=v.length();
for( char i='1';i<='9';i++)
{
for(j=0;j<x;j++)
{
if(v.charAt(j)==i)
{
b++;
break;
}
}
}
if(b==9)
{
System.out.println("fascinating number");
}
else
{
System.out.println("not fascinating number");
}
}
}

Output :
EX : 19 UNIQUE DIGIT NUMBER
CODING :
import java.io.*;
class unique_digit
{
public static void main(int m,int n)
{
int k=0,e=0,i=0,r=0,j=0,t=0,c=0,d=0;
t=n;
while(t>0)
{
t/=10;
c++;
}

int a[]=new int[c];


for(i=m;i<=n;i++)
{

t=i;
for(j=0;j<c;j++)
{
r=t%10;
a[j]=r;
t/=10;
}
for(k=1;k<10;k++)
{
for(j=0;j<c;j++)
{
if(k==a[j])
{
d++;
break;
}
}
}
if(d==4)
{
System.out.println(i);
e++;
d=0;
}
}
System.out.println("Frequency "+e);
}
}
Output :
EX.20 SPHENIC NUMBER
Coding:
import java.util.*;
class sphenic
{
void check(int n)
{
int c=0,c1=0,c2=0;
System.out.println("Prime factors:");
for(int i=2;i<=n;i++)
{
c=0;c1=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
c++;

}
}
if(c==2)
{
if(n%i==0)
{
System.out.println(i);
++c1;
c2++;
}
}
}
if(c2==3)
{
System.out.println("It is a sphenic number");
}
else
{
System.out.println("Not a sphenic number");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number");
int n=in.nextInt();
sphenic ob=new sphenic();
ob.check(n);
}
}
Output:
EX 21 ANAGRAM WORD
Coding:
import java.util.*;
class Anagram
{
public static void main()
{
int p1,p2,i,j,as1,as2,s1=0,s2=0;
String str1="",str2="",str3="",str4="";
char ch1,ch2;
Scanner in=new Scanner(System.in);
System.out.println("Enter first word");
str1=in.next();
str1=str1.toUpperCase();
System.out.println("Enter second word");
str2=in.next();
str2=str2.toUpperCase();
p2=str2.length();
p1=str1.length();
if(p1==p2)
{
for(i=65;i<=90;i++)
{
for(j=0;j<p2;j++)
{
ch1=str1.charAt(j);
ch2=str2.charAt(j);
as1=(int)ch1;
as2=(int)ch2;
if(as1==i)
{
str3=str3+ch1;
s1=s1+as1;
}
if(as2==i)
{
str4=str4+ch2;
s2=s2+as2;
}
}
}
if(str3.equals(str4)&&(s1==s2))
System.out.println(str1+" and "+str2+" are Anagram words");
else
System.out.println(str1+"and"+str2+"are not anagram words");
}
else
System.out.println("Wrong Input!!Re-enter words for Anagram");
}
}
Output:
Ex no. 22 ENCRYPTING CODE
Coding:
import java.util.*;
class Encryptingcode
{
public static void main()
{
int i,l,a=0,s;
String str1,str2="";
char chr;
Scanner in=new Scanner (System.in);
System.out.println("Enter coded text");
str1=in.next();
System.out.println("Enter shift");
s=in.nextInt();
if((s<1)||(s>26))
System.out.println("Invalid entry");
else
{
l=str1.length();
for(i=0;i<l;i++)
{
chr=str1.charAt(i);
a=(int)chr+(s-1);
if((char)a=='Q')
{
if(str1.charAt(i+1)+(s-1)=='Q'&&i<1)
{
a=32;
i++;
}
}
if(a>90)
a=a-26;
str2=str2+(char)a;
}
}
System.out.println("Decoded Text: "+str2);
}
}

Output:
EX:23 COMBINATION OF LETTERS
Coding:-
import java.util.*;
import java.io.*;
class comb
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String st;
System.out.println("Enter a String");
st=sc.next();
int i,j,k,l,m=st.length();
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
for(k=0;k<m;k++)
{
for(l=0;l<m;l++)
{
if(i!=j&&j!=k&&k!=l&&l!=i&&k!=i&&j!=l)
{
System.out.print(st.charAt(i)+""+st.charAt(j)+""+st.charAt(k)+""+st.charAt(l));
}
}
System.out.println();
}

}
}
}
}
Output:-
Ex no:24 ROUNDROBIN

Coding:
import java.io.*;
import java.util.*;
class Conversion
{
void modify(String wd)
{
int i,p,k;
char ch;
k=wd.length();
System.out.println("The modified String");
for(i=0;i<k;i++)
{
ch=wd.charAt(i);
if(ch>='a'&&ch<='x')
{
p=(int)(ch+2);
ch=(char)(p);
}
else if(ch>='y'&&ch<='z')
{
p=(int)(ch-25);
ch=(char)(p);
}
else if(ch>='A'&&ch<='X')
{
p=(int)(ch+2);
ch=(char)(p);
}
else if(ch>='Y'&&ch<='Z')

{
p=(int)(ch-25);
ch=(char)(p);
}
System.out.print(ch);
}
}
public static void main()
{
String str;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word");
str= sc.next();
Conversion ob = new Conversion();
ob.modify(str);
}
}
Output:
Ex. No.25 PIGLATIN WORD
Coding:
import java.util.*;
class Piglatin
{
int len;
String txt;
Piglatin()
{
txt=" ";
len=0;
}
void readstring()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a word in upper case:");
txt=in.nextLine();
}
void convert()
{
int i;
StringTokenizer s=new StringTokenizer(txt);
len = txt.length();
while(s.hasMoreTokens())
{
for(i=0;i<len;i++)
{
char c=txt.charAt(i);
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
break;
}
String s1=txt.substring(i,len);
String s2=txt.substring(0,i);
System.out.println("Piglatin form of "+txt+" is: "+(s1+s2+"AY"));
break;
}
}
void consonant()
{
len=txt.length();
int f=0;
for(int i=0;i<len;i++)
{
char ch=txt.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
continue;
else
f++;
}
System.out.println("No.of consonants in the word is: "+f);
}
public static void main()
{
Piglatin ob=new Piglatin();
ob.readstring();
ob.convert();
ob.consonant();
}
}
Output:

You might also like