Thursday, November 3, 2011

Learning C - C Primer Plus

I thought I would post some chapter review programs from chapters I'm reading in C Primer by Stephen Prata. This won't mean anything to anyone unless they know C programming. I'm basically putting it here so others can learn from what I've done and partly because I think a computer program is beautiful. Sort of in an artistic way. The characters and code seem like a picture to me.

This C program converts Centimeters to Feet and Inches:

//*************************************************
#include <stdio.h>
#define CMPERFT 30.48

float cm,ft,in;

int main (void) {
printf("Enter a height in centimeters (<=0 to quite): ");
scanf("%f", &cm );

while (cm > 0) {
ft = cm/CMPERFT;
in = (ft - (int)ft) * 12; //You can't find a remainder of a float with modulus.So use Cast
printf("\n%0.2f cm = %d feet, %0.3f inches\n", cm, (int)ft ,in);
printf("Enter a height in centimeters (<=0 to quite): ");
scanf("%f", &cm );
}

return 0;
}


//************************************************

No comments: