int main (void)
{
int fahrenheit; // fahrenheit stands for fahrenheit
double c; // c stands for celsius
printf("Enter your fahrenheit, we'll covnvert it into celsius! ");
scanf("%f", &fahrenheit);
c = 5/9 * (fahrenheit - 32);
printf("Here is your %f in celsius!.\n");
return (0);
}
I've followed the code through break points and when it takes in my input the calculations are off, but the formula is correct. Some sort of logic error I can't put my finger on. Please help!
The scanf call uses the wrong format string. You are reading an int so you need it to be:
scanf("%d", &fahrenheit);
The expression 5/9 is evaluated using integer division. In fact the compiler can work it out at compile time. That expression evaluates to 0.
You need to perform floating point division. For instance:
5.0/9
Or:
5/9.0
Or
5.0/9.0
You just need at least one operand to be a floating point value.
Putting this into your expression, you can write:
c = 5.0/9.0 * (fahrenheit - 32);
and obtain the answer that you expect.
Your printf statement is wrong too. You should enable warnings and let the compiler tell you that. You meant to write:
printf("Here is your %f in celsius!.\n", c);
Integer math versus floating point math.
i = 5/9 // i is equal to 0
d = 5.0/9.0 // d is equal to whatever 5 divided by 9 would actually be
You also need to actually print the value:
printf("Here is your %f in celsius!.\n", c);
Short answer: Operations on integers return integers even if the variable you store the result on is a double. Divisions between integers are truncated.
You should write this instead:
c = 5.0/9.0 * (fahrenheit - 32.0);
Adding a ".0" (or even just a ".") to your constant makes them floating point values.
Related
I need some help with a program for converting Fahrenheit to Celsius in C. My code looks like this
#include <stdio.h>
int main(void)
{
int fahrenheit;
double celsius;
printf("Enter the temperature in degrees fahrenheit:\n\n\n\n");
scanf("%d", &fahrenheit);
celsius = (5 / 9) * (fahrenheit - 32);
printf("The converted temperature is %lf\n", celsius);
return 0;
}
Every time I execute it it the result is 0.000000. I know I'm missing something but can't figure out what.
5/9 will result in integer division, which will = 0
Try 5.0/9.0 instead.
You problem is here :
celsius = (5/9) * (fahrenheit-32);
5/9 will always give you 0. Use (5.0/9.0) instead.
try celsius = ((double)5/9) * (fahrenheit-32); Or you can use 5.0.
The fact is that "/" looks at the operand type. In case of int the result is also an int, so you have 0. When 5 is treated as double, then the division will be executed correctly.
write 5/9.0 instead of 5/9 -- this forces double division
You need to use floating point arithmetic in order to perform these type of formulas with any accuracy. You can always convert the final result back to an integer, if needed.
When dealing with floats, it needs to be 5.0f / 9.0f.
When dealing with doubles, it needs to be 5.0 / 9.0.
When dealing with integers, remainders/fractions are always truncated. 5 / 9 results between 0 and 1, so it is truncated to just 0 every time. That multiplies the other side by zero and completely nullifies your answer every time.
I have my ZC706 board with dual arm cortex 32bit one. I am trying to run an algorithm over it, as a part of the code it has floating point values.
uint32_t int Ts = 69912;
float Ts_pico;
Ts_pico = 20*(10^-12)*Ts;
printf("Time stamp in picoseconds is %f", Ts_pico);
And it prints a value 4272595456.000000 instead of 1.39824*(10^-6)
So I tested by printing
printf("The float point value is %f", 1.39824);
It was fine printing out the following value.
Next, when i tested by printing
double f = 10^-6;
printf("The flloat point value is %f", f);
The value it has printed is -14401872.000000
How can I solve the issue with floating point values?
Didn't you want 10e-12 rather 10^-12?
10e-12 is a floating point double constant, although do note that 1e-12 is 10 raised to the -12th power. Your ^ is an abuse of the XOR operator.
wanna divide 2 numbers and get the result like this:
5 / 2 = 2.50
But it only outputs 2.
I don't now what i'm doing wrong.
Here my code:
int a;
int b;
int c;
printf("First num\n");
scanf("%d", &a);
printf("Second num\n");
scanf("%d", &b);
c = a / b;
printf("%d", c);
You need a double variable to store the result. int stores only integers. Additionally, you have to typecast the other variables also before performing the division.
Do something like this
double c;
.
.
.
c = (double)a / (double)b;
printf("%f", c);
NOTE:
You do not need the & in printf() statements.
To avoid the typecast in float you can directly use scanf with %f flag.
float a;
float b;
float c;
printf("First number\n");
scanf("%f", &a);
printf("Second number\n");
scanf("%f", &b);
c = a / b;
printf("%f", c);
The '/' - sign is for division. Whenever in C language, you divide an integer with an integer and store the data in an integer, the answer as output is an integer. For example
int a = 3, b = 2, c = 0;
c = a/b; // That is c = 3/2;
printf("%d", c);
The output received is: 1
The reason is the type of variable you have used, i.e. integer (int)
Whenever an integer is used for storing the output, the result will be stored as integer and not a decimal value.
For storing the decimal results, C language provide float, double, long float and long double.
Whenever you perform an operation and desires an output in decimal, then you can use the above mentioned datatypes for your resultant storage variable. For example
int a = 3, b = 2;
float c = 0.0;
c = (float)a/b; // That is c = 3/2;
printf("%.1f", c);
The output received: 1.5
So, I think this will help you to understand the concept.
Remember: When you are using float then the access specifier is %f. You need to convert your answer into float, just as I did, and then the answer will be reflected.
You have to use float or double variables, not int (integer) ones. Also note that a division between two integers will lead to an integer result, meanwhile a division between a float/double and an integer will lead to a float result. That's because C implicitly promote this integer to float.
For example:
5/2 = 2
5/2.0f = 2.5f
Note the .0f, this actually means that we are dividing with a float.
In C, only an int type number is displayed. 5/2 gives a floating point type number. So, the compiler compiles it only with the integer value.
I need some help with a program for converting Fahrenheit to Celsius in C. My code looks like this
#include <stdio.h>
int main(void)
{
int fahrenheit;
double celsius;
printf("Enter the temperature in degrees fahrenheit:\n\n\n\n");
scanf("%d", &fahrenheit);
celsius = (5 / 9) * (fahrenheit - 32);
printf("The converted temperature is %lf\n", celsius);
return 0;
}
Every time I execute it it the result is 0.000000. I know I'm missing something but can't figure out what.
5/9 will result in integer division, which will = 0
Try 5.0/9.0 instead.
You problem is here :
celsius = (5/9) * (fahrenheit-32);
5/9 will always give you 0. Use (5.0/9.0) instead.
try celsius = ((double)5/9) * (fahrenheit-32); Or you can use 5.0.
The fact is that "/" looks at the operand type. In case of int the result is also an int, so you have 0. When 5 is treated as double, then the division will be executed correctly.
write 5/9.0 instead of 5/9 -- this forces double division
You need to use floating point arithmetic in order to perform these type of formulas with any accuracy. You can always convert the final result back to an integer, if needed.
When dealing with floats, it needs to be 5.0f / 9.0f.
When dealing with doubles, it needs to be 5.0 / 9.0.
When dealing with integers, remainders/fractions are always truncated. 5 / 9 results between 0 and 1, so it is truncated to just 0 every time. That multiplies the other side by zero and completely nullifies your answer every time.
I am so confused with Modulus in C. I am writing a small script that allows the user to input their two number vars, then they can either add, subtract, multiply, divide (easy) or modulus (haven't caught this one yet). What would I be doing wrong in this? I get the "invalid operands to binary %" error, which means I need to format it to an int since it is a float. However what is the best way of doing this with the following? Any C help would be greatly appreciated.
int main (void)
{
float number1, number2, result;
char symbol;
//allow user interaction
printf("Enter your formula: \n");
scanf("%f %c %f", &number1, &symbol, &number2);
switch (symbol) {
case '%':
result = number1 % number2;
printf("Result: %f \n", result);
break;
default:
printf("Operation Error. Program aborted. \n \n");
break;
}
printf("Press any key to continue \n");
getchar();
return 0;
}
Where and how do I convert this?
You can either use a statement like:
result = (int)number1 % (int)number2;
to cast your floats to ints and perform the modulus operation, but you'll lose the decimal precision.
You could also include math.h and use fmod
result = fmod(number1, number2);
I recommend to use the fmod function of the standard C library.
The % operator only works on integer types. To perform the same task with floating points, you'd want to do something like:
#include <math.h>
float modulus(float a, float b)
{
return a - b * floor(a / b);
}
Mod is an integer operation, so it cannot be used on floats. Your case should read something like:
result = (int)number1 % (int)number2;
which will convert both floats to integers, and perform the mod. However, note that you are losing precision by casting a floating point number to an integer, so it is likely the result may not be what you'd expect.
change
case '%':
result = number1 % number2;
to
case '%':
result = (int)number1 % (int)number2;
?
modulo division is for integers. Cast each operand to an integer.