Sunday, 20 March 2016

Sorting numbers



class SortString{
 static String arr[]={
"13com50","13com48","13com64","13com41","13com52",
     "13com38","13com55","13com64","13com36","13com57"
                 
                      };
     public static void main(String args[]) {
               for(int j=0;j<arr.length;j++){
                for (int i=j+1;i<arr.length;i++) {
                 if(arr[i].compareTo(arr[j])<0) {
       String t=arr[j];
       arr[j]=arr[i];
       arr[i]=t;
      }
    }
  }
System.out.println("NAMES IN ASCENDING ORDER");
        for(int k =0; k<arr.length; k++)
      System.out.println(arr[k]);

System.out.println("NAMES IN DECENDING ORDER");
for(int k = arr.length-1;k>=0;k--)
            System.out.println(arr[k]);
   }
}
 
 OUTPUT:                                                                                                                   

H:\>javac ascendingorder.java
H:\>java SortString
NAMES IN ASCENDING ORDER
13com36
13com38
13com41
13com48
13com50
13com52
13com55
13com57
13com64
13com64
NAMES IN DECENDING ORDER
13com64
13com64
13com57
13com55
13com52
13com50
13com48
13com41
13com38
13com36



matrix multiplication



import java.io.*;
class matrmul
{
            public static void main(String args[])throws IOException
{
int i,n,m,j,x,y,a[][],b[][],c[][];

           
DataInputStream br =new DataInputStream(System.in);

System.out.println("Enter the number of rows for A");
m=Integer.parseInt (br.readLine());

System.out.println("Enter the number of columns for A");
n=Integer.parseInt (br.readLine());
a=new int[m][n];
System.out.println("Enter the A array Elements");
  for (i=0; i<m; i++)
  {
    for (j=0;j<n;j++)
    {
      a[i][j]=Integer.parseInt(br.readLine());
    }
  }

System.out.println("Enter the number of rows for B");
x=Integer.parseInt (br.readLine());

System.out.println("Enter the number of columns for B");
y=Integer.parseInt (br.readLine());

b=new int[x][y];
System.out.println("Enter the B array Elements");
  for (i=0; i<m; i++)
  {
   for (j=0;j<n;j++)
   {
     b[i][j]=Integer.parseInt(br.readLine());
   }
  }




if(n==x)                                                                                                           
{
c=new int[m][y];

for(int i =0; i<m;i++)
            {
   for(j=0;j<y;j++)
                        {
                                    for(int k =0; k<y;k++)
                                                {
                                                            c[i][j]= c[i][j]+a[i][k]*b[k][j];
                                                }
                                    }
                         }
            for(i=0;i<m;i++)
                        {
                                    for(j=0;j<y;j++)
                                                {
                                                            System.out.println(c[i][j]+"/t");
                                                }
                                                            System.out.println("/n");
                        }
}
else
            {
            System.out.println("Matrix Multiplication is not possible");
            }
}
}


OUTPUT


H:\>cd java

H:\Java>path=C:\jdk1.3\bin;

H:\Java>javac matrmul.java
Note: matrmul.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.



H:\Java>java matrmul                                                                   
Enter the number of rows for A
2
Enter the number of columns for A
2



Enter the A array Elements
1
2
3
4
Enter the number of rows for B
2
Enter the number of columns for B
2
Enter the B array Elements
1
2
3
4
Product is
7
10


15
22


matrix addition



CODING:
import java.io.*;
class matadd
{
public static void main(String args[])throws IOException
{
int i,n,m,j,x,y;
int a[][]=new int [10][10];
int b[][]=new int [10][10];
int c[][]=new int [10][10];
           
DataInputStream br =new DataInputStream(System.in);

System.out.println("Enter the number of rows for A");
m=Integer.parseInt (br.readLine());

System.out.println("Enter the number of columns for A");
n=Integer.parseInt (br.readLine());

System.out.println("Enter the number of rows for B");
x=Integer.parseInt (br.readLine());

System.out.println("Enter the number of columns for B");
y=Integer.parseInt (br.readLine());

if((m==x)&&(n==y))
{
  System.out.println("Enter the A array Elements");
  for (i=0; i<m; i++)
  {
    for (j=0;j<n;j++)
    {
      a[i][j]=Integer.parseInt(br.readLine());
    }
  }
  System.out.println("Enter the B array Elements");
  for (i=0; i<m; i++)
  {
   for (j=0;j<n;j++)
   {
     b[i][j]=Integer.parseInt(br.readLine());
   }
  }


       
    for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
      c[i][j]=a[i][j]+b[i][j];
    }
  }
  System.out.println("Answer");
  for (i=0;i<m;i++)
  {
    System.out.println("\t\n");
    for (j=0;j<n;j++)
    {
  System.out.println(c[i][j]);
  System.out.println("\t");
    }
    }
}
else
{
   System.out.println("Matrix Addition can not be performed for the given rows and columns of the matrices");
}
}
}

Output :

H:\>javac matadd.java
H:\>java matadd
Enter the number of rows for A
3
Enter the number of columns for A
3
Enter the number of rows for B
3
Enter the number of columns for B
3


Enter the A array Elements                                                                 
1
2
3
4
5
6
7
8
9

Enter the B array Elements
1
2
3
4
5
6
7
8
9

Answer
2
4
6

8
10
12

14
16
18