Page 167 - DCAP210_INTRODUCTION__TO_MICROPROCESSORS
P. 167

Unit 11: Counters and Time Delays


            11.6 Programs Using If Loops Counters Accumulators                                    Notes


            Program to print possible sum combinations of a number
            class NumberCombinations
            {
              void makeCombinations(int n)
              {
                  int sum,x,y,z,p;
                  for(x=1 ; x<=9 ; x++)
                  {
                      for(y=x ; y<=9 ; y++)
                      {
                          sum =x+y;
                          if(sum==n)
                              System.out.println(x + “  +  “ + y);
                          for(z=1 ; z<=9 ;  z++)
                          {
                              sum=sum+z;
                              if(sum<n)
                                  continue;
                              else
                                  break;
                          }//for ends
                           if(sum==n)
                          {
                             System.out.print(x + “  +  “ + y);
                             for(p=1 ; p<=z ; p++)
                                     System.out.print(“  +  “ + p);
                             System.out.println();
             }//if ends
                        }//for ends
                    }//for ends
                }//method ends
            }//class ends

            11.7 Hexadecimal Counter


            Hexadecimal means 16 and we are using what is called ‘Base 16’ when we count in Hex
            (Hexadecimal). Base 16 means that each column can hold up to 16 numbers before it has to be
            zeroed out, and 1 added to the next column to the left. We can put any one of 16 numbers in
            Column 1 (zero and 1-9 and A-F).
            When we are counting and we reach F (the equivalent of 15 in decimal) in Column 1, Column 1 is
            full and we zero it out. We then add 1 to Column 2 and a put a 0 in Column 1.
            If we keep on counting in this manner, adding 1 to col 2 every time col 1 reaches F, we fill up cols
            1 and 2 to a value of FF.


                                             LOVELY PROFESSIONAL UNIVERSITY                                   161
   162   163   164   165   166   167   168   169   170   171   172