Hi,
I'm currently going through
this C tutorial, and here's the task I've been set as an exercise:
Quote:
2. Write a program that counts from 1 to 12 and prints the count and its inversion to 5 decimal
places for each count. This will require a floating point number.
1 1.00000
2 .50000
3 .33333
4 .25000
etc.
|
I thought it'd be fairly easy at first, but for some unknown reason my code doesn't work! Here it is:
Code:
#include <stdio.h>
int main()
{
int count;
float invert;
for(count = 1; count < 13; count++) {
invert = (1 / count);
printf("%d\t\t%8.5f\n", count, invert); }
return 0;
}
The output in terminal is this:
Code:
1 1.00000
2 0.00000
3 0.00000
4 0.00000
5 0.00000
6 0.00000
7 0.00000
8 0.00000
9 0.00000
10 0.00000
11 0.00000
12 0.00000
I'm not sure why this is happening, because it
is a float, and I've set the output to have 5 decimal places. Any help would be appreciated.
M.