Monday 15 February 2021

Pattern 31 & 32

# Way 1

public class P7 
{
	public static void main(String[] args) 
	{
		int i, j, k, n=5;		
		for( i=-n; i<=n; i++)
		{
			if(i<0)
				k=-i;
			else
				k=i;
			for(j=1; j<=k; j++)
				System.out.print("  ");
			for(j=0; j<2*(n-k)+1; j++)
				System.out.print("*"+" ");
		System.out.println();
		}
	}
}

# Way 2

import java.util.Scanner;
public class P2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(" Enter Rows : ");
		int row = sc.nextInt();
		for(int i=0; i<row; i++)
		{
			for(int j=1; j<=row-i; j++)
				System.out.print("  ");
			for(int j=1; j<=2*i-1; j++)
				System.out.print("* ");
		System.out.println();
		}
		for(int i=row; i>=0; i--)
		{
			for(int j=1; j<=row-i; j++)
				System.out.print("  ");
			for(int j=1; j<=2*i-1; j++)
				System.out.print("* ");
		System.out.println();	
		}
	}
}

OUTPUT

 	* 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        * 
# Way 1

import java.util.Scanner;
public class p3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print(" Enter Rows : ");
		int row = sc.nextInt();
		for(int i=row; i>=0; i--)
		{
			for(int j=1; j<=i; j++)
				System.out.print("  ");
			for(int j=1; j<=row-i; j++)
				System.out.print(" *"+"  ");
		System.out.println();	
		}
		for(int i=1; i<row; i++)
		{
			for(int j=1; j<=i; j++)
				System.out.print("  ");
			for(int j=1; j<=row-i; j++)
				System.out.print(" *"+"  ");
		System.out.println();	
		}
	}
}

# Way 2

public class P7 
{
	public static void main(String[] args) 
	{
		int i, j, k, n=5;
		for( i=-n; i<=n; i++)
		{
			if(i<0)
				k=-i;
			else
				k=i;
			for(j=1; j<=k; j++)
				System.out.print(" ");
			for(j=0; j<(n-k)+1; j++)
				System.out.print("*"+" ");
		System.out.println();
		}
	}
}

OUTPUT

     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     *

Wednesday 27 January 2021

Simple Matrix Printing Program

 1. Matrix printing without user interaction

public class M1 {
	public static void main(String[] args) 
	{
		int matrix[][] = {{1,2,3}, {4,5,6}, {7,8,9}};
		
		int row = matrix.length;
		int col = matrix[0].length;
		System.out.println("\nMatrix Printing...");
		
		for(int i=0; i<row;i++)
		{			
			for(int j=0; j<col;j++)
				System.out.print(matrix[i][j]+" ");			
			System.out.println();
		}		
	}
}
OUTPUT

Matrix Printing...
1 2 3 
4 5 6 
7 8 9 

 2. Matrix printing Using Scanner (Taking input from user) 

import java.util.Scanner;
public class M2 {
	public static void main(String[] args) {
	 Scanner s = new Scanner(System.in);
	 System.out.println("enter rows");
	 int m = s.nextInt();
	 System.out.println("enter coloumn");
	 int n = s.nextInt();	 
	 int mat[][] = new int[m][n];
	 System.out.println("enter elements..");
	 	for(int i=0; i<m;i++){
			for(int j=0; j<n;j++)
				mat[i][j]=s.nextInt();
		}
	 	System.out.println("................");
	 	for(int i=0; i<m;i++){
			for(int j=0; j<n;j++)
				System.out.print(mat[i][j] +" ");
			System.out.println();
		}
	}
}
OUTPUT

enter rows
3
enter coloumn
3
enter elements..
1
2
3
0
6
5
0
4
7
................
1 2 3 
0 6 5 
0 4 7 

Monday 4 January 2021

Matrix Addition, Subtraction, Multiplication methods

# Substraction OF MATRIX ELEMENTS USING METHOD

//NOTE:To add matrix elements, rows and coloumns of both matrices must be same. 
import java.util.Scanner;
public class SubMatrix {
	void Matrix()
	{
		Scanner s = new Scanner(System.in);
		 System.out.println("Enter matrix rows");
		 	int m = s.nextInt();
		 System.out.println("Enter matrix coloumn");
		 	int n = s.nextInt();	 
		 	int a[][] = new int[m][n]; 
		 	int b[][] = new int[m][n];
		 	int c[][] = new int[m][n];
		 System.out.println("Enter 1st matrix elements..");
		 	for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
					a[i][j]=s.nextInt();
			}		 
		 System.out.println("Enter 2nd matrix elements..");
		  for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
						b[i][j]=s.nextInt();
			}		
		 System.out.println("Substraction of two Matrices");
		 	for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
					c[i][j]=a[i][j]-b[i][j];
			}
		 	for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
					System.out.print(c[i][j] +" ");
				System.out.println();
			}
	}
	public static void main(String[] args) 
	{
		SubMatrix obj = new SubMatrix();
		obj.Matrix();
	}
}

OUTPUT

Enter matrix rows
2
Enter matrix coloumn
2
Enter 1st matrix elements..
9
8
7
6
Enter 2nd matrix elements..
4
3
2
1
Substraction of two Matrices
5 5 
5 5 
# ADDITION OF MATRIX ELEMENTS without METHOD(not taking input from user)

public class mAdd {
	public static void main(String[] args) 
	{
		int matrix1[][] = {{7,8,9},{6,5,4},{1,2,3}};
		int matrix2[][] = {{1,2,3},{4,5,6},{7,8,9}};
		int m=matrix1.length;
		int n=matrix1[0].length;
		int add[][]=new int[m][n];
		System.out.println("First matrix elements");
		for(int i=0; i<m;i++)
		{			
			for(int j=0; j<n;j++)
				System.out.print(matrix1[i][j]+" ");			
			System.out.println();
		}
		System.out.println("Second matrix elements");
		for(int i=0; i<m;i++)
		{			
			for(int j=0; j<n;j++)
				System.out.print(matrix2[i][j]+" ");			
			System.out.println();
		}
		System.out.println("Addition of two matrices");
		for(int i=0; i<m;i++)
		{
			for(int j=0; j<n;j++)
				add[i][j]=matrix1[i][j]+matrix2[i][j];
		}
	 	for(int i=0; i<m;i++)
		{
			for(int j=0; j<n;j++)
				System.out.print(add[i][j] +" ");
			System.out.println();
		}
	}
}


OUTPUT

First matrix elements
7 8 9 
6 5 4 
1 2 3 
Second matrix elements
1 2 3 
4 5 6 
7 8 9 
Addition of two matrices
8  10  12  
10  10  10  
8  10  12  


# MULTIPLICATION OF MATRIX ELEMENTS USING METHOD(taking input from user)

//NOTE:To Multiply matrix elements, rows and coloumns of both matrices must be same. 


import java.util.*;
public class MulMatrix {
	void mulMatrix()
	{
		Scanner s = new Scanner(System.in);
		 System.out.println("Enter matrix rows");
		 	int m = s.nextInt();
		 System.out.println("Enter matrix coloumn");
		 	int n = s.nextInt();	 
		 	int a[][] = new int[m][n]; 
		 	int b[][] = new int[m][n];
		 	int c[][] = new int[m][n];
		 System.out.println("Enter 1st matrix elements..");
		 	for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
					a[i][j]=s.nextInt();
			}		 
		 System.out.println("Enter 2nd matrix elements..");
		  for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
						b[i][j]=s.nextInt();
			}	
		 System.out.println("Multiplication of two Matrices");
		 	for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
				{
					c[i][j]=0;
					for(int k=0; k<n; k++)
						c[i][j]+=a[i][k]*b[k][j];
				}
			}
		 	for(int i=0; i<m;i++)
			{
				for(int j=0; j<n;j++)
					System.out.print(c[i][j] +" ");
				System.out.println();
			}
	}
	public static void main(String[] args) 
	{
		MulMatrix obj = new MulMatrix();
		obj.mulMatrix();
	}
}

OUTPUT

Enter matrix rows
3
Enter matrix coloumn
3
Enter 1st matrix elements..
1
2
3
4
5
6
7
8
9
Enter 2nd matrix elements..
9
8
7
6
5
4
3
2
1
Multiplication of two Matrices
30 24 18 
84 69 54 
138 114 90 

Wednesday 30 December 2020

Pattern 1, 2, 3 and 4

public class p10 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i<=j)
					System.out.print("* ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}

OUTPUT

* * * * * 
  * * * * 
    * * * 
      * * 
        * 
 
public class P30 
{
	public static void main(String[] args) 
	{
		for(int i=5; i>0; i--)
		{
			for(int j=1; j<=5;j++)
			{
				if(i>=j)
					System.out.print("* ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}

OUTPUT

* * * * * 
* * * *   
* * *     
* *       
*         
public class P28 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i>=j)
					System.out.print("* ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}

OUTPUT

*         
* *       
* * *     
* * * *   
* * * * * 
public class P29 
{
	public static void main(String[] args) 
	{
		for(int i=5; i>0; i--)
		{
			for(int j=1; j<=5;j++)
			{
				if(i<=j)
					System.out.print("* ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}

OUTPUT

        * 
      * * 
    * * * 
  * * * * 
* * * * *

Pattern 25, 26, 27, 28, 29 and 30

public class P22 
{
	public static void main(String[] args) 
	{
		for(int i=5; i>0; i--)
		{
			for(int j=5; j>0;j--)
			{
				if(i>=j)
					System.out.print(j+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
5 4 3 2 1 
  4 3 2 1 
    3 2 1 
      2 1 
        1 

public class p11 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i<=j)
					System.out.print(i+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
1 1 1 1 1 
  2 2 2 2 
    3 3 3 
      4 4 
        5 

public class p12 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i<=j)
					System.out.print(j+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}	
}
OUTPUT
1 2 3 4 5 
  2 3 4 5 
    3 4 5 
      4 5 
        5 

public class p17 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i<=j)
					System.out.print((char)(64+j)+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
A B C D E 
  B C D E 
    C D E 
      D E 
        E 
public class P21 
{
	public static void main(String[] args) 
	{
		for(int i=5; i>0; i--)
		{
			for(int j=5; j>0;j--)
			{
				if(i>=j)
					System.out.print(i+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
5 5 5 5 5 
  4 4 4 4 
    3 3 3 
      2 2 
        1 

public class p18 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i<=j)
					System.out.print((char)(64+i)+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
A A A A A 
  B B B B 
    C C C 
      D D 
        E 

Pattern 14, 15, 16, 17, 18 and 19

public class p14
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i>=j)
					System.out.print(j+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
1         
1 2       
1 2 3     
1 2 3 4   
1 2 3 4 5 
public class p15 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i>=j)
					System.out.print(i+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
1         
2 2       
3 3 3     
4 4 4 4   
5 5 5 5 5 
public class p16 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i>=j)
					System.out.print((char)(64+j)+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
A         
A B       
A B C     
A B C D   
A B C D E 
public class P17 
{
	public static void main(String[] args) 
	{
		for(int i=5; i>0; i--)
		{
			for(int j=5; j>0;j--)
			{
				if(i<=j)
					System.out.print(i+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
5         
4 4       
3 3 3     
2 2 2 2   
1 1 1 1 1 
public class P18 
{
	public static void main(String[] args) 
	{
		for(int i=5; i>0; i--)
		{
			for(int j=5; j>0;j--)
			{
				if(i<=j)
					System.out.print(j+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
5         
5 4       
5 4 3     
5 4 3 2   
5 4 3 2 1 
public class p16 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i>=j)
					System.out.print((char)(64+i)+" ");
				else
					System.out.print("  ");
			}
		System.out.println();
		}
	}
}
OUTPUT
A         
B B       
C C C     
D D D D   
E E E E E 

Thursday 24 December 2020

Pattern 12 & 13

import java.util.*;
public class p8 {
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("Enter How many rows you want?");
		int n=s.nextInt();
		
		for(int i=1; i<=n; i++)
		{
			for(int j=1; j<=n-i;j++)
			{
				System.out.print(" ");
			}	
			for(int j=1; j<=2*i-1;j++)
			{
				if(i%2==0 && (j==1||j==(i*2-1)))
					System.out.print("+");
				else
					System.out.print("*");
			}
		System.out.println();
		}
	}
}

OUTPUT
       *
      +*+
     *****
    +*****+
   *********
  +*********+
 *************
+*************+


import java.util.*;
public class p9 {
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("Enter How many rows you want?");
		int n=s.nextInt();
		
		for(int i=n; i>0; i--)
		{
			for(int j=1; j	<=n-i;j++)
			{
				System.out.print(" ");
			}	
			for(int j=1; j	<=2*i-1;j++)
			{
				if(i%2==0&&(j==1||j==(i*2-1)))
					System.out.print("+");
				else
					System.out.print("*");
			}
		System.out.println();
		}
	}
}

OUTPUT

*************
 +*********+
  *********
   +*****+
    *****
     +*+
      *

Sunday 20 December 2020

Pattern 8, 9, 10 and 11

public class p3 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i==j)
					System.out.print("1 ");
				else
					System.out.print("0 ");
			}
		System.out.println();
		}
	}
}
OUTPUT

1 0 0 0 0 
0 1 0 0 0 
0 0 1 0 0 
0 0 0 1 0 
0 0 0 0 1 

public class p5 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i<=j)
					System.out.print("1 ");
				else
					System.out.print("0 ");
			}
		System.out.println();
		}
	}
}
OUTPUT

1 1 1 1 1 
0 1 1 1 1 
0 0 1 1 1 
0 0 0 1 1 
0 0 0 0 1 
public class p6 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(j<=i)
					System.out.print("1 ");
				else
					System.out.print("0 ");
			}
		System.out.println();
		}
	}
}

OUTPUT

1 0 0 0 0 
1 1 0 0 0 
1 1 1 0 0 
1 1 1 1 0 
1 1 1 1 1 
public class p7 {
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				if(i==j)
					System.out.print("* ");
				else if(i<j)
					System.out.print("1 ");
				else
					System.out.print("o ");
			}
		System.out.println();
		}
	}
}

OUTPUT

* 1 1 1 1 
o * 1 1 1 
o o * 1 1 
o o o * 1 
o o o o * 
Pattern 1 & 2

Pattern 5, 6 and 7

public class p1 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				System.out.print("*  ");
			}
		System.out.println();
		}
	}
}

OUTPUT

*  *  *  *  *  
*  *  *  *  *  
*  *  *  *  *  
*  *  *  *  *  
*  *  *  *  * 
public class p2 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				System.out.print(i+"  ");
			}
		System.out.println();
		}
	}
}

OUTPUT

1  1  1  1  1  
2  2  2  2  2  
3  3  3  3  3  
4  4  4  4  4  
5  5  5  5  5  
public class p4 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)
		{
			for(int j=1; j<=5;j++)
			{
				System.out.print(j+"  ");
			}
		System.out.println();
		}
	}
}

OUTPUT

1  2  3  4  5  
1  2  3  4  5  
1  2  3  4  5  
1  2  3  4  5  
1  2  3  4  5

Thursday 17 December 2020

Pattern 23 and 24

public class P23 {
	public static void main(String[] args) 
	{
		for(int i=1; i<6; i++)
		{
			for(int j=i; j<6; j++)
			{
				System.out.print(j + " ");
			}
			for(int k=1; k<i; k++)
			{
				System.out.print(k + " ");
			}
			System.out.println();
		}
	}	
}

OUTPUT

1 2 3 4 5 
2 3 4 5 1 
3 4 5 1 2 
4 5 1 2 3 
5 1 2 3 4 
public class P24 
{
	public static void main(String[] args) 
	{
		for(int i=5; i>0; i--)
		{
			for(int j=i; j<6; j++)
			{
				System.out.print(j + " ");
			}
			for(int k=1; k<i; k++)
			{
				System.out.print(k + " ");
			}
			System.out.println();
		}
	}
}

OUTPUT

5 1 2 3 4 
4 5 1 2 3 
3 4 5 1 2 
2 3 4 5 1 
1 2 3 4 5 

Pattern 31 & 32