Example:
Before Swap
number1=45 number2=33
After Swap
number1=33 number2=45
class Swap {
public static void main(String[] args) {
int number1=45,number2=33;
number1 = number1 + number2;
number2 = number1 - number2;
number1 = number1 - number2;
System.out.println("After swapping, number1= " + number1 + " and number2= "
+ number2);
}
}
Thursday, May 19, 2011
Tuesday, May 17, 2011
Summation of elements of upper triangle of matrix
Example:
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBsum {
static void triangleBsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i>=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle B is:"+c);
}
public static void main(String[] args)
{
triangleBsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBsum {
static void triangleBsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i>=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle B is:"+c);
}
public static void main(String[] args)
{
triangleBsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find minimum number in upper triangle of matrix
Example:
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmin {
static void triangleBmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
{
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle B is:"+ min);
}
public static void main(String[] args)
{
triangleBmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmin {
static void triangleBmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle B is:"+ min);
}
public static void main(String[] args)
{
triangleBmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find maximum number in upper triangle of matrix
Example:
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmax {
static void triangleBmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle B is:"+ max);
}
public static void main(String[] args)
{
triangleBmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmax {
static void triangleBmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle B is:"+ max);
}
public static void main(String[] args)
{
triangleBmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Summation of elements of lower triangle of matrix
Example:
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAsum {
static void triangleAsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle A is:"+c);
}
public static void main(String[] args)
{
triangleAsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAsum {
static void triangleAsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle A is:"+c);
}
public static void main(String[] args)
{
triangleAsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find minimum number in lower triangle of matrix
Example:
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmin {
static void triangleAmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
{
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle A is:"+ min);
}
public static void main(String[] args)
{
triangleAmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmin {
static void triangleAmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle A is:"+ min);
}
public static void main(String[] args)
{
triangleAmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find maximum number in lower triangle of matrix
Example:
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmax {
static void triangleAmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle A is:"+ max);
}
public static void main(String[] args)
{
triangleAmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmax {
static void triangleAmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle A is:"+ max);
}
public static void main(String[] args)
{
triangleAmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Sort of single dimension array
public class arraydemosortsingle
{
public static void sort(int []x)
{
int i;
for( i=0;i=0;i--)
{
System.out.println(x[i]);
}
}
public static void main(String[] args)
{
sort(new int[] {44,22,77,55,22});
}
}
{
public static void sort(int []x)
{
int i;
for( i=0;i
{
System.out.println(x[i]);
}
}
public static void main(String[] args)
{
sort(new int[] {44,22,77,55,22});
}
}
To find minimum number in single dimensional array
public class arraydemominsingle
{
public static int min(int []x)
{
int min=x[0];
for(int i=1;i
{
if(x[i]
{
min=x[i];
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[]{45,33,56,32}));
}
}
{
public static int min(int []x)
{
int min=x[0];
for(int i=1;i
if(x[i]
min=x[i];
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[]{45,33,56,32}));
}
}
To find minimum number in double dimensional array
public class arraydemomindouble
{
public static int min(int [][]x)
{
int min=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
{
min=x[i][j];
}
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[][]{{45,33},{56,32},{53,67}}));
}
}
{
public static int min(int [][]x)
{
int min=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
min=x[i][j];
}
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[][]{{45,33},{56,32},{53,67}}));
}
}
Merge Sort
public class arraydemomergesort
{
public static void mergesort(int x[], int y[])
{
int i,j=0,z[];
z=new int [x.length+y.length];
for(i=0;i=0;i--)
{
System.out.println(z[i]);
}
}
public static void main(String[] args)
{
mergesort(new int[] {44,22,77,55,22},new int[]{22,59,64,77});
}
}
{
public static void mergesort(int x[], int y[])
{
int i,j=0,z[];
z=new int [x.length+y.length];
for(i=0;i
{
System.out.println(z[i]);
}
}
public static void main(String[] args)
{
mergesort(new int[] {44,22,77,55,22},new int[]{22,59,64,77});
}
}
To find maximum number in single dimensional matrix
public class arraydemomaxsingle
{
public static int max(int []x)
{
int max=x[0];
for(int i=1;imax)
{
max=x[i];
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[]{45,33,56,32}));
}
}
{
public static int max(int []x)
{
int max=x[0];
for(int i=1;i
{
max=x[i];
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[]{45,33,56,32}));
}
}
To find Maximum number in Double dimensional matrix
public class arraydemomaxdouble
{
public static int max(int [][]x)
{
int max=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
{
max=x[i][j];
}
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[][]{{45,33},{56,32},{53,67}}));
}
}
{
public static int max(int [][]x)
{
int max=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
max=x[i][j];
}
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[][]{{45,33},{56,32},{53,67}}));
}
}
Summation of Matrices
public class arraydemomatrixsum {
static void matrixsum(int x[][])
{
int i,j,c,b=0,a=0;
for(i=0;i<3;i++)
{ c=0;
System.out.print(" ");
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
c=c+x[i][j];
if(i==j)
{
b=b+x[i][j];
}
if(i+j==2)
{
a=a+x[i][j];
}
}
System.out.print(c);
System.out.println("");
}
System.out.print(a);
for(j=0;j<3;j++)
{ c=0;
for(i=0;i<3;i++)
{
c=c+x[i][j];
}
System.out.print(c+" ");
}
System.out.print(b);
}
public static void main(String[] args)
{
matrixsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
static void matrixsum(int x[][])
{
int i,j,c,b=0,a=0;
for(i=0;i<3;i++)
{ c=0;
System.out.print(" ");
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
c=c+x[i][j];
if(i==j)
{
b=b+x[i][j];
}
if(i+j==2)
{
a=a+x[i][j];
}
}
System.out.print(c);
System.out.println("");
}
System.out.print(a);
for(j=0;j<3;j++)
{ c=0;
for(i=0;i<3;i++)
{
c=c+x[i][j];
}
System.out.print(c+" ");
}
System.out.print(b);
}
public static void main(String[] args)
{
matrixsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Multiplication of matrices
public class arraydemomatrixmultiply {
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]=0;
for(k=0;k<3;k++)
{
z[i][j]= z[i][j]+x[i][k]*y[k][j];
}
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]=0;
for(k=0;k<3;k++)
{
z[i][j]= z[i][j]+x[i][k]*y[k][j];
}
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Addition of two matrics
public class arraydemomatrixadd {
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]= x[i][j]+y[i][j];
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]= x[i][j]+y[i][j];
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Intersection of two sets
public class arraydemointersect
{
static void intersect(int x[],int y[])
{
int i,j,k=0,z[];
z=new int[5];
for(i=0;i
{
for(j=0;j
{
if (x[i]==y[j]&&x[i]!=z[j])
{
z[k]=x[i];
System.out.println(z[k]);
k++;
}
}
}
}
public static void main(String[] args)
{
intersect(new int[] {44,22,77,55,22,33},new int[]{22,59,64,77});
}
}
{
static void intersect(int x[],int y[])
{
int i,j,k=0,z[];
z=new int[5];
for(i=0;i
for(j=0;j
if (x[i]==y[j]&&x[i]!=z[j])
{
z[k]=x[i];
System.out.println(z[k]);
k++;
}
}
}
}
public static void main(String[] args)
{
intersect(new int[] {44,22,77,55,22,33},new int[]{22,59,64,77});
}
}
Subscribe to:
Posts (Atom)