CS1101C Lab 1 (Even Week)

Binary to Decimal Conversion

The deadline for this lab question is Friday 22 February 2008, 23:59:59 hours.

The name of your C program file must be called bin2dec.c, files with any other name will not be marked.

Preliminary

Write a simple binary number to decimal number converter. You may find this website useful.

Sample Runs

Assuming that the executable is bin2dec, sample runs of the program are shown below. User input is denoted in bold.

$ gcc -Wall bin2dec.c -o bin2dec

$ ./bin2dec

Enter binary number (maximum 10 digits): 0
Equivalent decimal number: 0

$ ./bin2dec

Enter binary number (maximum 10 digits): 1
Equivalent decimal number: 1

$ ./bin2dec

Enter binary number (maximum 10 digits): 11
Equivalent decimal number: 3

$ ./bin2dec

Enter binary number (maximum 10 digits): 01010101
Equivalent decimal number: 85

$ ./bin2dec

Enter binary number (maximum 10 digits): 1111101000
Equivalent decimal number: 1000

$ ./bin2dec

Enter binary number (maximum 10 digits): 1111111111
Equivalent decimal number: 1023

$

Note and Ponder

  1. Assume that all user input is valid.

  2. Use %d instead of %i in your scanf format specifier. (Why?)

  3. Loops and the modulus operator may prove to be very useful.

  4. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  5. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 1205 times since 19-Feb-08 17:59:01 SGT. This is the 10th time it has been accessed today.

A total of 689 different hosts have accessed this document in the last 52 days; your host, suna0.comp.nus.edu.sg, has accessed it 2 times.