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 

Friday, 11 December 2020

Armstrong numbers from 1 to 1000

public class Armstrong1to1000 {
	public static void main(String[] args) {
		System.out.println("Amstrong Numbers From 1 to 1000");
		int a,r;
		for(int i=1; i<=1000;i++)
		{
			a=0;
			int n=i;
			while(n!=0) 
			{
				r=n%10;
				a=a+r*r*r;
				n=n/10;
			}
			if(a==i)
			{
				System.out.println(i);
			}
		}
	}
}

OUTPUT:

Amstrong Numbers From 1 to 1000
1
153
370
371
407

Armstrong number using while loop

import java.util.*;
public class Armstrong {
	public static void main(String[] args) 	{
		Scanner scan = new Scanner(System.in);
		System.out.println("\nEnter a Number: ");
		int n = scan.nextInt();
		int reminder,amstrong=0,temp=n;
			while(n!=0)			
			{
				reminder=n%10;
				amstrong=amstrong+reminder*reminder*reminder;
				n=n/10;
			}
		if(temp==amstrong)
		{
		System.out.println("\n"+temp+" is an amstrong number");
		}
		else
		{
		System.out.println("\n"+temp+" is not an amstrong number");
		}
	}
}

OUTPUT 1:

Enter a Number: 
591

591 is not an amstrong number

OUTPUT 2:

Enter a Number: 
370

370 is an amstrong number

Armstrong number using For loop

import java.util.Scanner;
public class ArmstrongFor {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("\nEnter a Number: ");
		int n = scan.nextInt();
		int reminder,amstrong=0,temp=n;
			for(;n>0;)			
			{
				reminder=n%10;
				amstrong=amstrong+reminder*reminder*reminder;
				n=n/10;
			}
		if(temp==amstrong)
		{
			System.out.println("\n"+temp+" is an amstrong number");
		}
		else
		{
			System.out.println("\n"+temp+" is not an amstrong number");
		}
	}
}

OUTPUT:

Enter a Number: 
591

591 is not an amstrong number

OUTPUT 2:

Enter a Number: 
370

370 is an amstrong number

Armstrong number using Method

import java.util.Scanner;
public class ArmstrongMethod 
{
	void Amg(int n)
	{
		int reminder,amstrong=0,temp=n;
		while(n!=0)			
		{
			reminder=n%10;
			amstrong=amstrong+reminder*reminder*reminder;
			n=n/10;
		}
	if(temp==amstrong)
		System.out.println("\n"+temp+" is an amstrong number");
	else	
		System.out.println("\n"+temp+" is not an amstrong number");
	}
	public static void main(String[] args) 
	{
		AmstrongMethod obj = new AmstrongMethod();
		Scanner scan = new Scanner(System.in);
		System.out.println("\nEnter a Number: ");
		int n = scan.nextInt();
		obj.Amg(n);
	}
}

OUTPUT:

Enter a Number: 
591

591 is not an amstrong number

OUTPUT 2:

Enter a Number: 
370

370 is an amstrong number

Monday, 7 December 2020

Factorial of Number without User Interaction

public class FactorialWithoutUserInteraction 
{
	public static void main(String[] args) 
	{
		int fact=1;
			for(int i=1;i<=8;i++)
			{
				fact=fact*i;
			}
	System.out.println("\n Factorial of 8 is = "+fact);
	}
}

Output:

Factorial of 8 is = 40320

Factorial of Number using Method

import java.util.*;
public class FactMethod 
{
	void fact(int n)
	{
		int fact=1;
		for(int i=1;i<=n;i++)
		{
			fact=fact*i;
		}
	System.out.println("\n Factorial of "+n+" is = "+fact);
	}
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		FactMethod obj = new FactMethod();
		obj.fact(number);
	}
}

Output:

Enter Number...
5

 Factorial of 5 is = 120

Factorial of Number using Do-While Loop

import java.util.*;
public class FactDoWhile 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1,i=1;
			do
			{
				fact=fact*i;
				i++;
			}while(i<=number);
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:

Enter Number...
6

 Factorial of 6 is = 720
 
 

Factorial of Number using While Loop

import java.util.*;
public class FactorialWhile 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1,i=1;
			while(i<=number)
			{
				fact=fact*i;
				i++;
			}
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:
Enter Number...
7

 Factorial of 7 is = 5040
 

Factorial of Number using Recursion

import java.util.*;
public class FactorialRecursion 
{
	int fact(int n)
	{
		if(n==0)
			return 1;
		else
			return(n*fact(n-1));
	}
	public static void main(String[] args) 
	{
		FactorialRecursion obj = new FactorialRecursion();
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1;
		fact = obj.fact(number);
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:

Enter Number...
4

 Factorial of 4 is = 24

Factorial of Number using For Loop

import java.util.*;
public class Factotial 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1;
			for(int i=1;i<=number;i++)
			{
				fact=fact*i;
			}
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:

Enter Number...
3

 Factorial of 3 is = 6
 

Pattern 31 & 32