Thursday, 9 June 2016

Java program to display triangle 1 23 456 78910

Here is the java program to display triangle 1 23 456 78910. 1 23 456 78910 java program is very important program to learn loops in java.

/* Display Triangle as follow :
   1
   2 3
   4 5 6
   7 8 9 10 ... N */

class Output1{
public static void main(String args[]){
int c=0;
int n = Integer.parseInt(args[0]);
loop1: for(int i=1;i<=n;i++){
loop2: for(int j=1;j<=i;j++){
      if(c!=n){
      c++;
      System.out.print(c+" ");
      }
      else
      break loop1;
       }
       System.out.print("\n");
       }
}
}


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();
        }
    }
}

Print This Pattern


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();
        }
    }
}

If You Find This Program Helpful Leave A Comment And Do Share With Friends.

You May Also Like : -




0 comments:

Post a Comment