Showing posts with label Array in Java. Show all posts
Showing posts with label Array in Java. Show all posts

Sunday, August 21, 2011

Multiplication of two matrixs in java


//Multiplication of two matrixs in java
import java.io.*;
class arraymul
{
 public static void main(String[] arg)
  {
   int m,n,p,q;
   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
   int a[ ][ ]=new int[30][30];
   int b[ ][ ]=new int[30][30];
   int c[ ][ ]=new int[30][30];
    try
    {
     System.out.print("Enter the number of  rows in first matrix: ");
      m=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of column in first matrix: ");
      n=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of  rows in second matrix: ");
     p=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of column in second matrix: ");
      q=Integer.parseInt(in.readLine());
    if(n==p)
    {
      System.out.print("Enter the value of elements of first array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        a[i][j]=Integer.parseInt(in.readLine());
        }
      }
System.out.print("Enter the value of elements of second array: ");    
      for(int i=0;i<p;i++)
      {
       for(int j=0;j<q;j++)
       {      
        b[i][j]=Integer.parseInt(in.readLine());
        }
      }
     for(int i=0;i< m;i++)
      {
       for(int j=0;j<q;j++)
         {     
          c[i][j]=0;
            for(int k=0;k<n;k++)
            {
                 c[i][j]+=a[i][k]*b[k][j];
             }
         }
      }
     System.out.println("First matrix:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(a[i][j]+" ");
        }
        System.out.println();
      }
     System.out.println("Second matrix:");
       for(int i=0;i<p;i++)
      {
       for(int j=0;j<q;j++)
       {      
        System.out.print(b[i][j]+" ");
        }
        System.out.println();
      }
      System.out.println("Multiplication of two matrixs:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<q;j++)
       {      
        System.out.print(c[i][j]+" ");
        }
        System.out.println();
      }
    }
     else
       {
      System.out.print("Multiplication is not possible ");
       }
    }
    catch(Exception e){ }
   }
}

Sum of two matrix in java


//Sum of two matrix in java
import java.io.*;
class arraysum
{
 public static void main(String[] arg)
  {
   int m,n;
   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
   int a[ ][ ]=new int[30][30];
   int b[ ][ ]=new int[30][30];
   int c[ ][ ]=new int[30][30];
    try
    {
     System.out.print("Enter the number of  rows: ");
      m=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of column: ");
      n=Integer.parseInt(in.readLine());  System.out.print("Enter the value of elements of first array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        a[i][j]=Integer.parseInt(in.readLine());
        }
      }
      System.out.print("Enter the value of elements of second array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        b[i][j]=Integer.parseInt(in.readLine());
        }
      }
      System.out.println("First matrix:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(a[i][j]+" ");
        }
        System.out.println();
      }
     System.out.println("Second matrix:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(b[i][j]+" ");
        }
        System.out.println();
      }

     for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        c[i][j]=a[i][j]+b[i][j];
        }
      }
      System.out.println("Sum of two matrixs:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(c[i][j]+" ");
        }
        System.out.println();
      }
    }
    catch(Exception e){ }
   }
}

Wednesday, August 17, 2011

Transpose two dimensional array in java


import java.io.*;
class arraytranspose
{
 public static void main(String[] arg)
  {
   int m,n;
   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
   int arr[ ][ ]=new int[30][30];
    try
    {
     System.out.print("Enter the number of row: ");
      m=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of column: ");
      n=Integer.parseInt(in.readLine());
    System.out.print("Enter the value of elements of array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        arr[i][j]=Integer.parseInt(in.readLine());
        }
      }
System.out.println("Two dimensionl array before transpose:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(arr[i][j]+" ");
        }
        System.out.println();
      }
System.out.println("Two dimensionl array after transpose:");
       for(int i=0;i<n;i++)
       {
         for(int j=0;j<m;j++)
         {      
           System.out.print(arr[j][i]+" ");
          }
        System.out.println();
      }
    }
    catch(Exception e){ }
   }
}

Two dimensional array in java

import java.io.*;
class array2
{
 public static void main(String[] arg)
  {
   int m,n;
   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
   int arr[][]=new int[30][30];
    try
    {
     System.out.print("Enter the number of row: ");
      m=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of column: ");
      n=Integer.parseInt(in.readLine());
  System.out.print("Enter the value of elements of array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        arr[i][j]=Integer.parseInt(in.readLine());
        }
      }
      System.out.println("Two dimensionl array:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(arr[i][j]+" ");
        }
        System.out.println();
      }
    }
    catch(Exception e){}
   }
}

How to sort one dimensional array in java


import java.io.*;
class sortarray
{
 public static void main(String[] arg)
  {
   int n, temp;
   BufferedReader in=new BufferedReader(new  InputStreamReader(System.in));
   int arr[]=new int[30];
    try
    {
 System.out.print("Enter the number of elements of array: ");
      n=Integer.parseInt(in.readLine());
     for(int i=0;i<n;i++)
      {
      System.out.print("Enter the value of"+(i+1)+" th elements of array: ");      
        arr[i]=Integer.parseInt(in.readLine());
      }
      System.out.println("Unsorted array list:");
      for(int i=0;i<n;i++)
      {
       System.out.print("  "+arr[i]);
      }
      for(int i=0;i<n;i++)                       
       {
          for(int j=i+1;j<n;j++)
           {
           if(arr[i]>arr[j])          
            {                  
             temp=arr[i];
              arr[i]=arr[j];
              arr[j]=temp;
             }
           }
        }    
        System.out.println();
        System.out.println("Sorted array list:");
        for(int i=0;i<n;i++)
        {
            System.out.print("  "+arr[i]);
        }
     }
    catch(Exception e){ }
   }
}

Next Home
Twitter Delicious Facebook Favorites More