Thursday, October 30, 2014

Java Matrices Pro



Java program to add two matrices

import java.util.Scanner;
class AddTwoMatrix
{
public static void main(String args[])
          {
      int m, n, c, d;
      Scanner in = new Scanner(System.in);
                System.out.println("Enter the number of rows and columns of matrix");
      m = in.nextInt();
      n  = in.nextInt();
      int first[][] = new int[m][n];
      int second[][] = new int[m][n];
      int sum[][] = new int[m][n];
      System.out.println("Enter the elements of first matrix");
         for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();
      System.out.println("Enter the elements of second matrix");
         for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            second[c][d] = in.nextInt();
         for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
             sum[c][d] = first[c][d] + second[c][d];   
      System.out.println("Sum of entered matrices:-");
         for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )
            System.out.print(sum[c][d]+"\t");
            System.out.println();
      }
   }
}

Java program to reverse number

import java.util.Scanner;
class ReverseNumber
{
   public static void main(String args[])
   {
      int n, reverse = 0;
      System.out.println("Enter the number to reverse");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      while( n != 0 )
      {
          reverse = reverse * 10;
          reverse = reverse + n%10;
          n = n/10;
      }
      System.out.println("Reverse of entered number is "+reverse);
   }
}

Java program to get ip address

import java.net.InetAddress;
class IPAddress
{
 public static void main(String args[]) throws Exception
{
      System.out.println(InetAddress.getLocalHost());
   }
}

Java program to perform garbage collection

import java.util.*;
class GarbageCollection
{
 public static void main(String s[]) throws Exception
{
      Runtime rs =  Runtime.getRuntime();
      System.out.println("Free memory in JVM before Garbage Collection = "+rs.freeMemory());
      rs.gc();
      System.out.println("Free memory in JVM after Garbage Collection = "+rs.freeMemory());
   }
}

Java program to generate random numbers

import java.util.*;
class RandomNumbers 
{
 public static void main(String[] args) 
{
    int c;
    Random t = new Random();
     for (c = 1; c <= 10; c++) 
    {
      System.out.println(t.nextInt(100));
    }
  }
}

Java program to display date and time, print date and time using java program

import java.util.*;
class GetCurrentDateAndTime
{
 public static void main(String args[])
 {
      int day, month, year;
      int second, minute, hour;
      GregorianCalendar date = new GregorianCalendar();
 
      day = date.get(Calendar.Day_of_month);
      month = date.get(Calendar.Month);
      year = date.get(Calendar.Year);
 
      second = date.get(Calendar.Second);
      minute = date.get(Calendar.Minute);
      hour = date.get(Calendar.Hour);
 
      System.out.println("Current date is  "+day+"/"+(month+1)+"/"+year);
      System.out.println("Current time is  "+hour+" : "+minute+" : "+second);
   }
} 

Java program to find all substrings of a string

import java.util.Scanner;
class SubstringsOfAString
{
public static void main(String args[])
{
      String string, sub;
      int i, c, length;
 
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a string to print it's all substrings");
      string  = in.nextLine();
 
      length = string.length();   
 
      System.out.println("Substrings of \""+string+"\" are :-");
 
      for( c = 0 ; c < length ; c++ )
      {
         for( i = 1 ; i <= length - c ; i++ )
         {
            sub = string.substring(c, c+i);
            System.out.println(sub);
         }
      }
   }
}

Java program for binary search

import java.util.Scanner;
class BinarySearch 
{
public static void main(String args[])
{
    int c, first, last, middle, n, search, array[];
 
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt(); 
    array = new int[n];
 
    System.out.println("Enter " + n + " integers");
      for (c = 0; c < n; c++)
      array[c] = in.nextInt();
 
    System.out.println("Enter value to find");
    search = in.nextInt();
 
    first  = 0;
    last   = n - 1;
    middle = (first + last)/2;
    while( first <= last )
    {
      if ( array[middle] < search )
        first = middle + 1;    
      else if ( array[middle] == search ) 
      {
        System.out.println(search + " found at location " + (middle + 1) + ".");
        break;
      }
      else
         last = middle - 1;
 
      middle = (first + last)/2;
   }
   if ( first > last )
      System.out.println(search + " is not present in the list.\n");
  }
}

Java program for linear search

import java.util.Scanner;
class LinearSearch 
{
public static void main(String args[])
{
    int c, n, search, array[];
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt(); 
    array = new int[n];
    System.out.println("Enter " + n + " integers");
      for (c = 0; c < n; c++)
      array[c] = in.nextInt();
    System.out.println("Enter value to find");
    search = in.nextInt();
    for (c = 0; c < n; c++)
    {
      if (array[c] == search)     /* Searching element is present */
      {
         System.out.println(search + " is present at location " + (c + 1) + ".");
          break;
      }
   }
   if (c == n)
      System.out.println(search + " is not present in array.");
  }
}

No comments:

Post a Comment