-6
#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
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
Gow.
  • 55
  • 9

3 Answers3

1
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.

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • Thanks, I understood the logical error. But when I use a printf("Enter a number") before scanf("%d", &a) and later printf("Your number is %d",a)..., but on the console I don't even get the message "Enter a number" and it waits till i enter some valve and then prints both the messages in the printf. What's happening..? – Gow. Jul 08 '15 at 14:38
  • @Gow It must be a problem with your eclipse installation. Try running it directly on your OS's console and see what happens, – Shreevardhan Jul 08 '15 at 14:40
  • Ahh.! Please tell me, How do you run it on OS's console..? – Gow. Jul 08 '15 at 14:46
  • @Gow. Why not read it yourself. To compile `gcc code.c`. To run on Linux `./a.out` and Windows `a.exe`. – Shreevardhan Jul 08 '15 at 16:09
0

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
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

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.

acwaters
  • 586
  • 3
  • 6