Back

Pattern 29

C
            #include<stdio.h>
              #include<conio.h>
              void main(){
                int i, j, n;
                clrscr();
                for(i=5;i>0;i--){
                   n=1;
                   printf("\n");
                   for(j=5;j>i;j--){
                printf("  ");
                   }
                   for(j=i;j>0;j--){
                if(n!=2)
                  n--;
                else
                  n++;
                printf(" %d  ",n);
             
                   }
                }
                getch();
             }
          
Output:
            0   -1   -2   -3   -4
              0   -1   -2   -3 
                0   -1   -2
                  0   -1
                    0
          
Download Program
Java
            public class Pgm29 {
              public static void main(String[] args) {
                // TODO Auto-generated method stub
                for(int i=5;i>0;i--){
                      int n=1;
                      System.out.print("\n");
                      for(int j=5;j>i;j--){
                  System.out.print("  ");
                      }
                      for(int j=i;j>0;j--){
                  if(n!=2)
                    n--;
                  else
                    n++;
                  System.out.print(" "+n+"  ");
                      }
                  }
              }
            }
          
Output:
            0   -1   -2   -3   -4
              0   -1   -2   -3 
                0   -1   -2
                  0   -1
                    0
          
Download Program
C++
            #include<iostream>
            using namespace std;
            int main(){
              int i, j,n;
              for(i=5;i>0;i--){
                n=1;
                cout<<"\n";
                for(j=5;j>i;j--){
                  cout<<"  ";
                }
                for(j=i;j>0;j--){
                if(n!=2)
                  n--;
                else
                  n++;
                cout<<" "<<n<<"  ";
                }
              }
              getc;
              return 0;
            }
          
Output:
            0   -1   -2   -3   -4
              0   -1   -2   -3 
                0   -1   -2
                  0   -1
                    0
          
Download Program