Not understanding the output when Converting Fahrenheit to Celsius [duplicate] - c

This question already has answers here:
C Temperature Conversion Program Keeps Outputting 0 For Fahrenheit to Celsius [duplicate]
(2 answers)
Closed 8 years ago.
I have following C program, that takes a temperature in Fahrenheit and convert in to Celsius value. But, every time I input a value it always gives me0.00 as output. I'm not understanding where is the problem.
#include <stdio.h>
int main()
{
float cel_out, fht_in;
printf("Enter a temperature in farenheit: ");
scanf("%f", &fht_in);
cel_out = (fht_in - 32) * (5/9);
printf("Temperature in celcious: %.2f", cel_out);
return 0;
}

In this euqation
cel_out = (fht_in - 32) * (5/9);
When you use 5/9 it is integer data type which results 0. so
cel_out = (fht_in - 32) * 0;
Results 0 only!
You need to use-
cel_out = (fht_in - 32) * (5.0f/9.0f);
here it is treated as float values and gives the actual result of 5/9.

Related

Getting incorrect output for pow function typecasted in int for 5,11,15... and so on raised to 2 [duplicate]

This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
other number number raised to 2 are giving correct answer but 5,11,15... are showing 1 number less than the correct answer.
#include<stdio.h>
#include<math.h>
int main(){
int side;
printf("Enter side value \n");
scanf("%d", &side);
printf("The area is %d", (int) pow(side,2.0));
return 0;
}

Why am I getting 0 as the output? Shouldn't it be 40? [duplicate]

This question already has answers here:
Why printf() isn't outputting this integer as float number? [duplicate]
(8 answers)
Closed 4 years ago.
Why does the following code output 0 instead of 40?
#include <stdio.h>
int main()
{
int volume;
int length = 5;
int width = 8;
volume = length * width;
printf("%f", volume);
return 0;
}
The variable "volume" is Integer , you need in the printf function to change from %f that is for float variable, to %d that is for print Integer variable.
Declaration of a variable is int volume; and you are printing it by format specifier %f which belongs to float type of variable. Type casting requires (float)volume. In C programming it happens a lot because compiler dependency comes in picture.

Trying to understand this printf formatting output [duplicate]

This question already has answers here:
Why does printf print wrong values?
(7 answers)
Closed 7 years ago.
I don't understand why this code produces the below output:
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
float c_to_f(float c);
int main(void) {
for (int c = LOWER; c <= UPPER; c += STEP) {
printf("%3.0f %6.1f\n", c, c_to_f(c));
}
return 0;
}
float c_to_f(float c) {
return c * (9.0/5.0) + 32;
}
with output:
0 5144477247317086170901765440027035767837163293591161256351693248184965237877467107389389528872273154691913581744607058050215827488351921876414407003384176234234181468372580859505320314312544948225387164490993094256968227227818959640206687395851530141696.0
0 5144477248223936133773425621301895611375254354002848388186656757998856300277376171732587490599187934010444965285645525890922748188992798901374640654313592657351174354641522290319226294263523595109393871854132336451448805097328901373303486131449817464832.0
0 5144477248704033172940775129035644940307184915397270987393402145547386862724386852855456999748731052473196874219136479453649941501096792620471234940099754293118994117960373636044235342472865231695044481634617817613232640440010635232001791344413616635904.0
0 5144477249104114038913566385480436047750460383225956486732356635171162331430229087124514924040016984525490131663712274089255935927850120719718396844921555656258843920726083090815076215980649928849753323118355718581385836558912080114250379021883449278464.0
0 5144477249344162558497241139347310712216425663923167786335729328945427612653734427685949678614788543756866086130457750870619532583902117579266693987814636474142753802385508763677580740085320747142578628008598459162277754230252947043599531628365348864000.0
0 5144477249584211078080915893214185376682390944620379085939102022719692893877239768247384433189560102988242040597203227651983129239954114438814991130707717292026663684044934436540085264189991565435403932898841199743169671901593813972948684234847248449536.0
I do understand that c should be a float, just not the output. Any help would be greatly appreciated.
The problem here, as I see it is
printf("%3.0f %6.1f\n", c, c_to_f(c));
c is an int and you're trying to print it's value using %f, which is undefined behavior.
Using an inappropriate type of argument for a format specifier is undefined behavior.
When using printf and floats you have the right token %f. But your using it wrong. To printf out 2 decimal places you would use %.2f, 3 would be %.3f and so on. I think the console is bugging out because you are using %3.0f and %6.1f

Basic C programming.. logical error while converting Celsius to kelvin [duplicate]

This question already has answers here:
C: converting Farenheit to Celsius
(3 answers)
Closed 6 years ago.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int celsius = 0, kelvin = 0;
float farenheit;
printf("Enter the Boiling of water in Celcius \n");
scanf("%d", &celsius);
kelvin = 273 + celsius;
farenheit = celsius * (9 / 5) + 32;
printf("The Boiling point of water in Kelvin is %d \n and in farenheit is %f \n \n", kelvin, farenheit);
printf("Enter the Freezing point of water in Celcius \n");
scanf("%d", &celsius);
kelvin = 273 + celsius;
farenheit = celsius * (9 / 5) + 32;
printf("The freezing point of water in Kelvin is %d \n and in farenheit is %f \n \n", kelvin, farenheit);
return 0;
}
In eclipse, I type this code and I'm expecting it to ask me on console "Enter the Boiling of water in Celsius" and wait till i type in 100 and again do the same for freezing point. But after I build(ctrl+B), it does nothing. I had to enter some number(Here i did '100')then press 'enter' and again some number (here its '1') then press 'enter'. It displayed me the calculations for '100' degree Celsius.And it didn't even ask me to enter the freezing point either. I typed '1' and pressed enter.
Whats wrong with my code.?
Here's my output.
100
1
Enter the Boiling of water in Celcius
The Boiling point of water in Kelvin is 373 and in farenheit is 132.000000
Enter the Freezing point of water in Celcius
The freezing point of water in Kelvin is 274 and in farenheit is 33.000000
int celsius;
scanf("%d", &celsius);
int kelvin = 273 + celsius;
float farenheit = 1.8 * celsius + 32; // To make it a float
Input
100
0
Output
212.000000 373
32.000000 273
See http://ideone.com/6ikg23 demo.
the 9/5 is an integer divide.
an integer divide drops any fraction.
So the result is 1.
To fix the problem, use float rather than int
9.0f/5.0f
First, the code you provided is missing a brace at the end and won't calculate the proper Fahrenheit conversion due to integer truncation; however, the first one can be chalked up to a copy-paste error, and the second is a common mistake among programmers (new and old).
You code compiled and ran perfectly fine for me, and after fixing the Fahrenheit error (hint: use a decimal point, (9./5), to calculate it in floating-point) it returned the correct values for my inputs.
Unfortunately, I'm not familiar with Eclipse, so I can't be much help there. But it's definitely a tooling issue, not a problem with your code.

Why does this code print greater? [duplicate]

This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 8 years ago.
1)#include<stdio.h>
int main()
{
float x=0.5;
if(x>0.5)
printf("\ngreater");
else
printf("\nlesser ");
return 0;
}
output->lesser
2)#include<stdio.h>
int main()
{
float x=0.1;
if(x>0.1)
printf("\ngreater ");
else
printf("\nlesser ");
return 0;
}
output->greater
Why in the first case output is "lesser" while in the second one output is "greater"? What is the difference?
EDIT: I understood that 0.1 is not equal, but then why 0.5 is showing as equal?
I almost sure it's because you are comparing float and double.
There's an answer to why it is greater answered here : 0.1 float greater than double

Resources