# Thread: Keeping the fractional part of a quotient

1. ## Keeping the fractional part of a quotient

I am working on a lab problem. It is: Write a program that calculates the speed of sound (a) in air of a given temperature T (in Fahrenheit). The formula to compute the speed in feet/second:
a = 1086 * sqrt ((5T+297)/247)
Be sure your program does not lose the fractional part of the quotient in the formula shown. As part of your solution, write and call a function that displays instructions to the program user.

I know how to do everything else but keep the fractional part. Below is my program. If anyone could help me out with this I'd really appreciate it. In another problem from this lab we used the scale function to round a number so I'm not sure if we might need to use this function again?

Code:
```#include <stdio.h>
#include <math.h>

int main (void)
{
float sound, temperature;
printf("Enter a temperature T in fahrenheit: ");
scanf("%f", &temperature);

sound = 1086 * (sqrt ((5 * temperature + 297) / 247));

printf("When the temperature is %f F, the speed of sound in the air is %f\n", temperature, sound);
return 0;
}```

2. What you have looks like it should keep the fractional parts alright, although just to be safe, I'd recommend specifying the constants as floating point numbers (5.0 instead of 5, 247.0 instead of 247, etc.).

Also, please use [CODE] tags when posting code.

3. First off, thanks for posting code! But you should use CODE tags (they save indentation and make it easier to read)

Code:
```#include <stdio.h>
#include <math.h>

int main (void)
{
float sound, temperature;
printf("Enter a temperature T in fahrenheit: ");
scanf("%f", &temperature);

sound = 1086 * (sqrt ((5 * temperature + 297) / 247));

printf("When the temperature is %f F, the speed of sound in the air is %f\n",     temperature, sound);
return 0;
}```
(isn't that better?)

Now, to your question...

Are you losing the fractional part? Did you try using a calculator? I ask because this problem has been around the Internet for a while. I think they may just be wanting you to use floats and nothing more. I compiled it as is and saw no loss in precision. However, it's good defensive code to do what Clairvoyant suggests (or to cast your integers to floats).

You may also want to put parenthesis around the 5*t term, just so that it's clear to someone that you intend to NOT add the 297 first. This is especially useful if you program professionally and you or others may look at this code years later.

4. I really apologize for the lack of tags in my original post. I'm a first time user of this thread and shamefully admit that I didn't read the rules well enough. My fault entirely, it won't happen again.

The wording threw me off with this problem and its context in my course. Thanks for all the help everyone!

Popular pages Recent additions