Sunday, 19 June 2016

Program to print a Number Pattern Type-1.

Explanation :
If n=6 then output will be
66666666666
65555555556
65444444456
65433333456
65432223456
65432123456
65432223456
65433333456
65444444456
65555555556
66666666666.

Code:

import java.util.Scanner;
public class Pattern11
{
    public static void main(String args[])
    {
        int i,j;
        System.out.print("Enter any number:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for (i=n; i >=1 ; i--)
        {
           for (j=n ; j >= i ; j--)
                System.out.print(j);
           for (j=1 ; j<(i*2)-1 ;j++)
                System.out.print(i);
           for (j=i+1;j<=n;j++)
                System.out.print(j);
           System.out.print("\n");
        }
      
        for (i=2;i<=n;i++)
        {
            for (j=n; j>=i; j--)
               System.out.print(j);
            for (j =1 ; j<(i*2)-1 ; j++)
               System.out.print(i);
            for (j=i+1 ;j<=n ;j++)
               System.out.print(j);
            System.out.print("\n");
        }
        sc.close();
}
}

Program to perform Multiplication of 2 Matrices.

Code:

import java.util.Scanner;
public class MatrixMultiplication
{
   public static void main(String args[])
   {
      int m, n, p, q, sum = 0, c, d, k;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();

      int first[][] = 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 number of rows and columns of second matrix");
      p = in.nextInt();
      q = in.nextInt();

      if ( n != p )
         System.out.println("Matrices with entered orders can't be multiplied with each other.");
      else
      {
         int second[][] = new int[p][q];
         int multiply[][] = new int[m][q];

         System.out.println("Enter the elements of second matrix");

         for ( c = 0 ; c < p ; c++ )
            for ( d = 0 ; d < q ; d++ )
               second[c][d] = in.nextInt();

         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
            {  
               for ( k = 0 ; k < p ; k++ )
               {
                  sum = sum + first[c][k]*second[k][d];
               }

               multiply[c][d] = sum;
               sum = 0;
            }
         }

         System.out.println("Product of entered matrices:-");

         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
               System.out.print(multiply[c][d]+"\t");

            System.out.print("\n");
         }
      }
   }
}

Program to find the Transpose of a Matrix.

Code:

import java.util.Scanner;
public class Transpose
   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 matrix[][] = new int[m][n]; 
         System.out.println("Enter the elements of matrix"); 
         for ( c = 0 ; c < m ; c++ ) 
             for ( d = 0 ; d < n ; d++ ) 
                matrix[c][d] = in.nextInt(); 
         int transpose[][] = new int[n][m]; 
         for ( c = 0 ; c < m ; c++ ) 
        {
            for ( d = 0 ; d < n ; d++ ) 
                transpose[d][c] = matrix[c][d]; 
         }
         System.out.println("Transpose of entered matrix:-"); 
        for ( c = 0 ; c < n ; c++ ) 
       { 
           for ( d = 0 ; d < m ; d++ ) 
               System.out.print(transpose[c][d]+"\t"); 
               System.out.print("\n");
        }
   } 
}

Program to Add or Subtract two Matrices.

Code:

import java.util.Scanner;
public class Add
{
     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]; //replace '+' with '-' to subtract matrices     
      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();
      }
  }
 }