Coding Help: Formatting Float Output in C

  • Thread starter Thread starter muzihc
  • Start date Start date
  • Tags Tags
    Float
AI Thread Summary
To modify the provided C program for better output formatting, the division of integers needs to be adjusted to ensure that the results are in decimal form. This can be achieved by casting the integers to floats before performing the division. The corrected lines should read: `div1 = (float)int1 / 2.;` and `div2 = (float)int2 / 2.;`. This change ensures that the division is treated as floating-point division, allowing for decimal results.Additionally, to control the number of decimal places and reduce excess zeros in the output, the format specifier in the `printf` function can be adjusted. Changing `%20f` to `%10.2f` will limit the output to two decimal places and reduce the overall width, improving readability. For further details on formatting options, consulting a C reference manual or resources on the `printf` function is recommended.
muzihc
Messages
15
Reaction score
0
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;
}
 
Technology news on Phys.org
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.
 
Thanks.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top