Pattern Program in java

Pattern Program in java

Pattern Program in java


Pattern print in java

Pattern programs are very good programs for learning loops. These kinds of programs asked in ICSE, ISC, CBSE, BCA exams. In this post, there is several numbers of java pattern program solved with output. Enjoy and learn the pattern in Java.

Floyd Triangle in Java

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 

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

54321
  4321
    321
      21
        1

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

- + - + -
+ - + -
- + -
+ -
-

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

1

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


public class Pattern1

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

1

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


public class Pattern2

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



12345

22345
33345
44445
55555


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


          1

     121
   12321
 1234321
123454321
  1234321
    12321
      121
        1


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

0

12
345
6789
543
21
0

class Pattern5
{
    public static void main()
    {
        int i,j,k=0,k2=5;
        for(i=1;i<=4;i++)
        {
            for(j=0;j<i;j++)
            {
                System.out.print(k);
                k++;
            }
            System.out.println();
        }
        for(i=3;i>=1;i--)
        {
            for(j=0;j<i;j++)
            {
                System.out.print(k2);
                k2--;
            }
            System.out.println();  
        }
    }
}
   

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

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

Java program for GCD with Recursion Technique

Java program for GCD with Recursion Technique

Java program for GCD with Recursion Technique

Java program for GCD/HCF with Recursion Technique


Greatest Common divisor or GCD/HCF or Highest Common Factor or Greatest Common Factor of two or more integers number when at least one of them is not zero is the largest positive integer that divides the numbers without a remainder. For example, the GCD of two numbers 54 and 24 is :6.


First Method import java.io.*;
public class GCDRecursive
{
    private int a, b, g;  // Instance variable
    public int gcd(int x,int y)
    {
        while(x!=y)
        {
            if(x>y)
                return gcd(x-y , y);
            else
                return gcd(x , y-x);
        }
        return x;
    }

    public static void main(String args[]) throws IOException
    {
        int a,b,gcd,lcm;
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        System.out.println("Enter two number");
        a = Integer.parseInt(br.readLine());
        b = Integer.parseInt(br.readLine());
        // Object of class GCDRecursive
        GCDRecursive ob=new GCDRecursive();
        gcd=ob.gcd(a,b);
        System.out.println("GCD="+gcd);
    }

}


Output
Enter two number
54
24
GCD=6

Enter two number
10
3
GCD=1

Second Method
import java.util.Scanner;
public class GreatestCommonDivisor {
    public static void main(String args[]) {
        //int n1 = 366, n2 = 60;
        int n1,n2,gcd;
       // int hcf = hcf(n1, n2);
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Two number");
        n1=sc.nextInt();
        n2=sc.nextInt();
        gcd=greatestCommonDivisor(n1,n2);

        System.out.println("G.C.D of %d and %d is %d.", n1, n2, gcd);
    }

    public static int greatestCommonDivisor(int n1, int n2)
    {
        if (n2 != 0)
            return greatestCommonDivisor(n2, n1 % n2);
        else
            return n1;
    }
}
Output
Enter Two number
54
24
G.C.D of 54 and 24 is 6


Core Java: An Integrated Approach, New: Includes All Versions upto Java 8