Thread Closed

Using float in C

 
Share Thread Thread Tools
Sep16-08, 12:19 AM   #1
 

Using float in C


I'm very new to C. If I had a program like the one below, I was hoping someone could tell me how I should edit it so that the lines "printf("\n%20s%20f", "Half of 1", div1);" and "printf("\n%20s%20f", "Half of 2", div2);" output decimals without too many excess spaces. Currently when outputted, they are in decimal form but don't give remainders and have quite a few zeros. I'm really stuck here and have spent way too much time on this. I'd really appreciate any help.



#include<stdio.h>

int main(void)
{
int int1, int2;
float div1, div2;

printf("\nGive two integers: ");
scanf("%d%d", &int1, &int2);

div1 = int1 / 2
div2 = int2 / 2

printf("\n%20s%20d", "First Number", int1);
printf("\n%20s%20d", "Second Number", int2);
printf("\n%20s%20f", "Half of 1", div1);
printf("\n%20s%20f", "Half of 2", div2);

return 0;
}
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Ants and carnivorous plants conspire for mutualistic feeding
>> Forecast for Titan: Wild weather could be ahead
>> Researchers stitch defects into the world's thinnest semiconductor
Sep16-08, 01:20 AM   #2
 
Recognitions:
Homework Helper Homework Help
The division will be an integer division, change the code to:

div1 = (float)int1 / 2.;
div2 = (float)int2 / 2.;

Note if either number in a binary operation is a float, then the other number will be "promoted" to a float as well.

If you have a c reference manual, look up type promotion.
Sep16-08, 11:51 AM   #3
 
Thanks.
Sep18-08, 12:19 AM   #4
zyh
 

Using float in C


Here is another issue about "printf"
see http://www.google.com/search?hl=en&q=printf&aq=f&oq= for a detail reference.
If you want to limit the strengh of output. You could easily change the to

%10f which means you should limit the output to 10 bit spaces.
Thread Closed
Thread Tools


Similar Threads for: Using float in C
Thread Forum Replies
Sink or float? Introductory Physics Homework 12
Float a lantern Engineering Systems & Design 1
shouldn't it float?? Classical Physics 39
I can't float General Discussion 16
will it float? Introductory Physics Homework 4