Program by Luck..

Jul 28, 2009 19:48

While implementation the Pascal Triangle, I observed some symmetry pattern in the output of the program. Afterward I made some change in the actual Pascal code, like printing only a specific digit else white space and converting double digit to single digit (n%10);

Uses : java Pascal

Pascal.javapublic class Pascal{

public static void main(String[] args){

int n = Integer.parseInt(args[0]);
                int p = Integer.parseInt(args[1]);
                String str =  args[2].trim();

for(int i=0;i                        String out="";
                        for(int k=0;k<(n-i);k++)
                                out+= " ";

for(int j=0; j < (i+1); j++){
                                Float f = new Float((fac(i)/(fac(j)*fac(i-j)))+"");
                                int nn  = f.intValue()%10;

if(Integer.parseInt(args[1]) == -1)
                                        out += nn+ " ";

else if( nn == p){
                                        out += str+ " ";
                                }
                                else
                                        out += "  ";
                        }
                        System.out.println(out);
                }
        }
        static float fac(int n){
                float f=1;
                for(int i=1;i<=n;i++)
                        f*=i;
        return f;
        }
}

1st parameter:    Number of iteration  [eg. 50]
2nd parameter:  which number want to replace [eg: 4,6, 7]
3rd parameter:  which character want to print; [eg: * ,^, !]

  • java Pascal 50 4 '*'
                                                *   *  
                                               *     *      
                                                  *            
                                      *   *               *   *  
                                     * * * *   * * * *   * * * *  
                                        *                   *          
                                         *                 *            
                                      *   *               *   *          
                                           *             *                
                            *   *   *   *   *   *   *   *   *   *   *   *  
                                            *   * * *   *                      
                           *             *                 *             *      
                                                *   *                            
                                           *             *                        
                                    *     * *           * *     *                  
                                           *             *                          
                  *         *   *                                   *   *         *  
  • java Pascal 50 6 '^'
                                                      ^    
                                                  ^       ^  
                                                    ^   ^      
                                                 ^   ^ ^   ^    
                                              ^               ^    
                                               ^     ^ ^     ^      
                                        ^                           ^  
                                         ^       ^         ^       ^    
                                          ^                       ^      
                                           ^                     ^        
                                                      ^                    
                                                    ^   ^                    
                                                   ^     ^                    
                                  ^   ^   ^       ^   ^   ^       ^   ^   ^    
                              ^                 ^           ^                 ^  
                                ^                                           ^      
                             ^           ^ ^   ^     ^ ^     ^   ^ ^           ^    
                                  ^     ^                           ^     ^          
                                       ^ ^         ^     ^         ^ ^                
                          ^     ^   ^       ^       ^ ^ ^       ^       ^   ^     ^    
                           ^   ^     ^     ^ ^                 ^ ^     ^     ^   ^      
                              ^   ^   ^ ^                           ^ ^   ^   ^          
  • java Pascal 50 7 '!'
                                             !         !  
                                               !     !          
                                            ! !       ! !          
                                   !                             !  
                                          !               !          
                                       ! !                 ! !            
                         !                 ! !         ! !                 !  
                                                ! ! !                                
                 ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !  
                    ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !      
                     ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !        
                        ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !            
                           ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !                
                                  ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !                        
                                         ! ! ! ! ! ! ! ! ! !

symmetry, pascal, program, java

Previous post Next post
Up