chandra shekhar (pinch_hitter ) wrote,
  • Mood: cheerful

Program by Luck..

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 <iteration> <specific digit below 10> <any special character>

Pascal.java
public 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<n;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 '!'
                                             !         !  
                                               !     !          
                                            ! !       ! !          
                                   !                             !  
                                          !               !          
                                       ! !                 ! !            
                         !                 ! !         ! !                 !  
                                                ! ! !                                
                 ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !  
                    ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !      
                     ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !        
                        ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !            
                           ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !                
                                  ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !                        
                                         ! ! ! ! ! ! ! ! ! !                   
             


Tags: java, pascal, program, symmetry
  • Post a new comment

    Error

    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments