Wednesday, 22 June 2016

Program to print a Number Pattern Type-28(Pascal Triangle).

Explanation :
If n =6 then output will be
              1
            1  1
          1  2  1
        1  3  3  1
     1  4   6   4  1
    1 5 10 10  5  1.

Code:

import java.util.Scanner;
public class Pattern28
{
    public static void main( String args[])
    {
        int line,i,j;
        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter the no. of lines: ");
        line=sc.nextInt();
       
    
        for(i=0;i<line;i++){
             for(j=0;j<line-i-1;j++)
                 System.out.printf(" ");
    
             for(j=0;j<=i;j++)
                 System.out.print(fact(i)/(fact(j)*fact(i-j)));
             System.out.printf("\n");
        }
    }
    
   public static long fact(int num){
        long f=1;
        int i=1;
        while(i<=num){
             f=f*i;
             i++;
      }
      return f;
     }
}

Program to print a Number Pattern Type-27.

Explanation :
If n=6 then output will be
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36.

Code:

import java.util.Scanner;
public class Pattern27
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d ",i*j);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-26.

If n=5 then output will be
11111
0000
111
00
1 .

Code:

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

Program to print a Number Pattern Type-25.

Explanation :
If n=5 then output will be
1
10
101
1010
10101.

Code:

import java.util.Scanner;
public class Pattern25
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d",j%2);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-24.

Explanation :
If n=5 then output will be
                       1
                   2  3
               4  5  6
          7  8  9 10
 11 12 13 14 15 .

Code:

import java.util.Scanner;
public class Pattern24
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        k=1;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=1;j--)
            {
                if(j > i)
                        System.out.printf("   ");
                else
                        System.out.printf("%3d",k++);
            }
            System.out.printf("\n");
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-23.

Explanation :
If n=5 then output will be
  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15 .

Code:

import java.util.Scanner;
public class Pattern23
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        k=1;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=i;j--)
            {
                System.out.printf("%3d",k++);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-22.

Explanation :
If n=5 then output will be
 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15  .

Code:

import java.util.Scanner;
public class Pattern22
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=1;
         for(int i = 1 ; i <= n ; i++)
         {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(" "+k++);
             }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-21.

Explanation :
If n=6 then output will be
            1
          123
        12345
      1234567
    123456789
1234567891011
    123456789
     1234567
       12345
         123
           1  .

Code:

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

Program to print a Number Pattern Type-20.

Explanation :
If n=6 then output will be
                         1  
                     1  2  3
                1  2  3  4  5
            1  2  3  4  5  6  7
        1  2  3  4  5  6  7  8  9
 1  2  3  4  5  6  7  8  9  10  11  .

Code:

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

Program to print a Number Pattern Type-19.

Explanation :
If n=6 then output will be
          1
        1 2
      1 2 3
    1 2 3 4
  1 2 3 4 5
1 2 3 4 5 6 .

Code:

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

Program to print a Number Pattern Type-18.

Explanation :
If n=6 then output will be
        1
      2 2
     3 3 3
    4 4 4 4
   5 5 5 5 5
 6 6 6 6 6 6 .

Code:

import java.util.Scanner;
public class Pattern12
{
    public static void main(String args[])
    {
        System.out.print("Enter the number of Rows:");
        Scanner sc=new Scanner(System.in);
        int z=sc.nextInt();
        int i, k, x,j=5;
        for (i=1;i<=z;i++)
        {
            for (k=1;k<=j;k++)
            {
                System.out.printf(" ");
            }
            for (x=1;x<=i;x++)
            {
                System.out.print(i);
                System.out.printf(" ");
            }
            System.out.printf("\n");
            j=j-1;
        }
    }
}

Program to print a Number Pattern Type-17.

Explanation :
If n=6 then output will be
111111
22222
3333
444
55
6.

Code:

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

Program to print a Number Pattern Type-16.

Explanation :
If n=6 then output will be
1
22
333
4444
55555
666666.

Code:

import java.util.Scanner;
public class Pattern16
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-15.

Explanation :
If n=6 then output will be
654321
65432
6543
654
65
6.

Code:

import java.util.Scanner;
public class Pattern15
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = n; j >= i ; j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-14.

Explanation :
If n=6 then output will be
654321
54321
4321
321
21
1.

Code:

import java.util.Scanner;
public class Pattern14
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = n ; i >= 1 ; i--)
        {
            for(int j = i; j >= 1 ; j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-13.

Explanation :
If n=6 then output will be
1
23
345
4567
56789
67891011.

Code:

import java.util.Scanner;
public class Pattern13
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1;i<=n;i++)
        {
            j=i;
            for (k=1;k<=i;k++)
            {
                System.out.printf("%d",j++);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-12.

Explanation :
If n=6 then output will be
1                    1
12                21
123            321
1234        4321
12345    54321
123456654321.

Code:

import java.util.Scanner;
public class Pattern12
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1;i<=n;i++) {
            for (j=1;j<=n;j++) {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            for (j=n;j>=1;j--)
            {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-11.

Explanation :
If n=6 then output will be
          1
        21
      321
    4321
  54321
654321.

Code:

import java.util.Scanner;
public class Pattern11
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for (int i=1;i<=n;i++)
        {
            for (int j=n;j>=1;j--)
            {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-10.

Explanation :
If n=6 then output will be
1
21
321
4321
54321
654321.

Code:

import java.util.Scanner;
public class Pattern10
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = i; j >= 1 ; j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-9.

Explanation :
If n=6 then output will be
123456
12345
1234
123
12
1
12
123
1234
12345
123456.

Code:

import java.util.Scanner;
public class Pattern6
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ck=0,c=2;
        while(c>0)
        {
           if(ck==0)
           {
              for(int i = 1,r = n ; i <= n ; i++,r--)
              {
                 for(int j = 1; j <= r; j++)
                 {
                     System.out.print(j);
                 }
                     System.out.println();     
               }
               ck++;
            }
           else
           {
              for(int i = 2;i <= n; i++)
              {
                 for(int j = 1;j <= i ; j++)
                 {
                    System.out.print(j);         
                 }
                    System.out.println();
              }
           }
               c--;
         }
        sc.close();
    }
}

Program to print a Number Pattern Type-8.

Explanation :
If n=6 then output will be
1
12
123
1234
12345
123456
12345
1234
123
12
1.

Code:

import java.util.Scanner;
public class Pattern8
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ck=0,c=2;
        while(c>0)
        {
            if(ck == 0)
            {
              for(int i = 1;i <= n ; i++)
              {
                for(int j =1 ; j <= i  ; j++)
                {
                  System.out.print(j);
                }
                  System.out.println();
               }
               ck++;     
             }
            else
            {
               for(int i =1 , r = n-1 ; i <= n - 1 ; i++ ,r--)
               {
                  for(int j = 1 ; j <= r; j++)
                  {
                     System.out.print(j);
                  }
                     System.out.println();
                }
             }
               c--;
      }
        sc.close();
    }
}

Program to print a Number Pattern Type-7.

Explanation :
If n=6 then output will be
123456
  23456
    3456
      456
        56
          6.

Code:

import java.util.Scanner;
public class Pattern7
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
         int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
          for(int k = n - i ;  k < n - 1 ; k++)
          {
            System.out.print(" ");
          }
          for(int j = i ; j <= n ; j++)
          {
            System.out.print(j);
          }
           System.out.println();
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-6.

Explanation :
If n=6 then output will be
123456
12345
1234
123
12
1.

Code:

import java.util.Scanner;
public class Pattern6
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i =1, r = n; i <= n ; i++ ,r--)
        {
            for(int j = 1 ; j <= r ; j++)
            {
                System.out.print(j);
            }
            System.out.println();      
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-5.

Explanation :
If n=6 then output will be
1
12
123
1234
12345
123456.

Code:

import java.util.Scanner;
public class Pattern5
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1; i <= n ; i++)
        {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-4.

Explanation :
If n=6 then output will be
666666
566666
456666
345666
234566
123456.

Code:

import java.util.Scanner;
public class Pattern4
{
   
        public static void main( String args[])
        {
            System.out.print("Enter the number of rows:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int i,j,k;
            for (i=n;i>=1;i--)
            {
                k = i;
                for (j=1;j<=n;j++)
                {
                    if(k <= n)
                    {
                        System.out.printf("%d",k);
                    } else
                    {
                        System.out.print(n);
                    }
                    k++;
                }
                System.out.printf("\n");
            }
            sc.close();
        }
}

Program to print a Number Pattern Type-3.

Explanation :
If n=4 then output will be
1111
1   1
1   1
1111.

Code:

import java.util.Scanner;
public class Pattern3
{
   
        public static void main( String args[])
        {
            System.out.print("Enter the number of rows:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int i,j;
            for (i=1;i<=n;i++)
            {
                for (j=1;j<=n;j++)
                {
                    if(j==n || j==1 || i==1 || i==n)
                            System.out.printf("1");
                    else
                            System.out.printf(" ");
                }
                System.out.printf("\n");
            }
            sc.close();
        }
}

Program to print a Number Pattern Type-2.

Explanation :
If n=6 then output will be
 111111
100001
100001
100001
100001
 111111.

Code:

import java.util.Scanner;
public class Pattern2
{
        public static void main( String args[])
        {
            System.out.print("Enter the number of rows:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int i,j;
            for (i=1;i<=n;i++)
            {
                for (j=1;j<=n;j++)
                {
                    if(j==n || j==1 || i==1 || i==n)
                        System.out.printf("1");
                    else
                        System.out.printf("0");
                }
                System.out.printf("\n");
            }
            sc.close();
        }
}