book exercise

This is a discussion on book exercise within the C# Programming forums, part of the General Programming Boards category; the following exercise is from a book im learning from... Write two programs that each a table of the binary, ...

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    book exercise

    the following exercise is from a book im learning from...

    Write two programs that each a table of the binary, octal, and hexadecimal equivalents of the decimal numbers in the range of 1-256

    a) for the first program, print the results to the console without using any string formats.

    b) for the second program, print the results to the console using both the deciamal and hexadecimal string formats (there are no formats for binary and octal in C#).

    i have completed the octal conversion for the first program ...

    Code:
    using System;
    
    
    class Test
    {
    	
    	
    	static void Main(string[] args)
    	{
    		int octal = 1;
     
    		Console.WriteLine("Decimal\t\tOctal");
    
    		for(int dec=1; dec<=256; dec++, octal++)
    		{
    			Console.Write(dec + "\t\t");//decimal
    			
    			if(dec%8==0)
    				octal += 2;
    
    			switch(dec)
    			{
    				case 64: octal = 100;
    					 break;
    				
    				case 128: octal = 200;
    					 break;
    
    				case 192: octal = 300;
    					 break;
    
    				case 256: octal = 400;
    					 break;
    			
    			}//end switch
    
    			Console.Write(octal);
    			Console.WriteLine();
    			
    		}//end loop
    
    		Console.WriteLine("Voila!");
    	}
    	
    }
    this works, i guess the code could be more efficient, with regards to the binary and hex conversions i was hoping to get some help with the algorithm or at least point me in the right direction please

    luigi

  2. #2
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    more efficient code

    i have reduced the code as follows for the octal conversion

    Code:
    using System;
    
    
    class Test
    {
    	
    	
    	static void Main(string[] args)
    	{
    		int octal = 1; int octtemp = 100;
     
    		Console.WriteLine("Decimal\t\tOctal");
    
    		for(int dec=1; dec<=256; dec++, octal++)
    		{
    			Console.Write(dec + "\t\t");//decimal
    			
    			if(dec%8==0 && dec%64!=0)
    				octal += 2;
    
    
    			else
    				if(dec%64 == 0)
    			{
    				octal = octtemp;
    				octtemp += 100;
    			}
    
    			Console.Write(octal);
    			Console.WriteLine();
    			
    		}//end loop
    
    		Console.WriteLine("Voila!");
    	}
    	
    }
    now going to work out hex algo .. hmmm how am i going to produce the letters A-F ?

    luigi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with the exercise from the book
    By kenryuakuma in forum C++ Programming
    Replies: 19
    Last Post: 09-26-2008, 09:07 AM
  2. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 02:24 PM
  3. Exercise from my book.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 12-05-2002, 08:54 AM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  5. any recommended exercise book for C++?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2001, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21