Sunday, 19 February 2017

Java Program To Print Pattern (Important Pattern Program)

Pattern Program In Java:    Java programs to print patterns are one of the frequently asked interview programs mostly for freshers or students. Because they t
est the candidate’s logical ability as well as coding skills which are ‘must have skills’ for any software engineer. In this post, we have collected some of the different number pattern programs in java and have tried to solve them. 
Java Program To Print Pattern
Java Program To Print Pattern
Java Program To Print Pattern 
Pattern -1

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



public class series {
public static void main (String args[]){

for(int i=1;i<=5;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println();
}
for(int i=1;i<5;i++){
for(int j=5;j>4-i;j--){
System.out.print(j);
}
System.out.println();
}
}
}

Output

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Java Program To print This pattern

Pattern-2
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1


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

}

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


Java Program To Print This Pattern
Pattern-3
-------- 1
------2 1 2
--- 3 2 1 2 3
--4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
-- 4 3 2 1 2 3 4 
- --- 3 2 1 2 3
- - ---- 2 1 2
-- -- ---- 1

public class series {
    public static void main ( String arg[] ){
            int num = 5;
            //upper triangle
            for(int i=1;i<=num;i++){
                for(int j=1;j < num-(i-1);j++){
                    System.out.print("-");
                }
                for(int k=i;k>=1;k--){
                    System.out.print(k);
                }
                for(int k1=2;k1<=i;k1++){
                        System.out.print(k1);
                    }
                System.out.println();
            }
            //lower triangle    
            for(int i=1;i<=num-1;i++){
                for(int j=i;j>=1;j--){
                    System.out.print("-");
                }
                for(int k=num-i;k>=1;k--){
                    System.out.print(k);
                    
                }
                for(int k1=2;k1<=num-i;k1++){
                        System.out.print(k1);
                    }
                System.out.println();     
            }
    }
}
Output

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

Java Program To Print This Pattern

Pattern-4

1
121
12321
1234321
123454321
1234321
12321
121
1

class series {
    public static void main(String Args[]) {
        int max = 5;    //max no. in series
        //Upper Triangle
        for(int i=1;i<=max;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j);
            }
            for(int k=i-1;k>=1;k--){
                System.out.print(k);
            }
            System.out.println();
        }
        //Lower Triangle
        for(int i=1;i<=max-1;i++){
            for(int j=1;j<=max-i;j++){
                System.out.print(j);
            }
            for(int k=max-i-1;k>=1;k--){
                System.out.print(k);
            }
            System.out.println();
        }
    }

}

Output
1
121
12321
1234321
123454321
1234321
12321
121
1


Java Program To Print This Pattern
Pattern-5
Pattern-5(1)
T
TE
TEA
TEAC
TEACH
TEACHE
TEACHER

Pattern-5(2)

TEACHER
--EACHER
----ACHER 
------CHER
---------HER
-----------ER
-------------R 

Code For Pattern 1:
class series{
    public static void main(String args[]){
        String txt = "TEACHER";
        for(int i=1;i<=txt.length();i++){
            System.out.println(txt.substring(0, i));
        }
    }
}

Output
T
TE
TEA
TEAC
TEACH
TEACHE
TEACHER


Code for Pattern 2:
class series{
    public static void main(String args[]){
        String txt = "TEACHER";
        for(int i=0;i<txt.length();i++){
            for(int s=i;s>0;s--)    System.out.print("-");
            System.out.println(txt.substring(i, txt.length()));
        }
    }
}

Output
TEACHER
--EACHER
----ACHER 
------CHER
---------HER
-----------ER
-------------R 

Java Program to Print This pattern

Pattern-6
A
A B A 
A B C B A
A B C D C B A
A B C B A
A B A
A

public class series {
    public static void main ( String arg[] ){
        String txt = "ABCD";
        for(int i=0;i<txt.length();i++){
            for(int s=1;s<txt.length()-i;s++) System.out.print(" ");
            for(int j=0,k=i;j<=i*2;j++){
                if(j<=i) System.out.print(txt.substring(j, j+1)+" ");
                else{
                    System.out.print(txt.substring(k-1, k)+" ");
                    k--;
                }
            }
            System.out.println();
        }
        for(int i=0;i<txt.length();i++){
            for(int s=-1;s<i*2;s++) System.out.print(" ");
            for(int j=txt.length()-i-1,n=0;j>0;j--,n++)
                System.out.print(txt.substring(n, n+1)+" ");
            for(int j=txt.length()-i-2;j>0;j--)
                System.out.print(txt.substring(j-1, j)+" ");
            System.out.println();
        }
    }
}

Output
A
A B A 
A B C B A
A B C D C B A
A B C B A
A B A
A


Java Program To Print This Pattern

Pattern-7

999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
Java Program To Print Pattern
Java Program To Print Pattern
333            333
22              22
1                1

public class CurveOutput {

    public static void main(String args[]) {
        int startNumber = 9;

        for (int currentNum = startNumber; currentNum >= 0; currentNum--) {
            StringBuilder line = new StringBuilder();
            for (int i = 0; i < currentNum; i++) {
                line.append(currentNum);
            }
            for (int i = 0; i < startNumber - currentNum; i++) {
                line.append(" ");
            }
            System.out.println(line.toString() + line.reverse().toString());
        }
    }
}

Output

999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1

You May Also Like :- How To Reverse An Array In Java

0 comments:

Post a Comment