I'm trying to convert temperature given in Fahrenheit to Celsius. But for some reason its not working properly. I know that a similar question has been asked but my problem is different because I can't even printout what I have scanned (using scanf) from the user.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float Fahrenheit, Celsius;
clrscr();
printf("Enter Temperature in Fahrenheit \n");
scanf("%f",&Fahrenheit);
Celsius = 5.0/9.0 * (Fahrenheit-32);
printf("\n Temperature in Fahrenheit = %f", Fahrenheit);
printf("\n Temperature in Celsius = %f", Celsius);
getch();
}
Output :
I'm using Windows 7 - 64 bit. IDE = Emulated C++ 3.0
I'm not sure (because your compiler might behave differently) but perhaps it's caused by this:
You're using 5.0 and 9.0 which are double values as well as 32 which is an int.
Try changing them to 5.0f, 9.0f and 32.0f.
It seems to be a compiler issue. My compiler (Emulated Turbo C++ 3.0) was not able to save my edits properly. So I went to C:\TC\Bin\ filename.c and opened the file in Notepad. Corrected the errors and compiled it again.
Now it works :)
Related
I am a beginner to coding trying to learn C. I tried to make a program which would convert celsius to fahrenheit. When I tried to run this code:
#include <stdio.h>
int main()
{
// Define celsius and fahrenheit.
float celsius;
float far = (celsius * 9 / 5) + 32;
// Ask for Celsius temperature
printf("What is the temperature in celsius?\n");
// Get the input of the temperature
scanf("%f", &celsius);
// Convert to Fahrenheit
//Print the temperature in Farenheit.
printf("The temperature converted to Farenheit is %f\n", far);
return 0;
}
It gives the output (no error / warning):
What is the temperature in celsius?
5
The temperature converted to Farenheit is -1172181139767515960282508306484822016.000000
Which is wrong as it should be 41. Also, every time I run this, I get a different temperature.
However, running this code (defining far after scanf), there are no issues:
#include <stdio.h>
int main()
{
// Define celsius
float celsius;
// Ask for Celsius temperature
printf("What is the temperature in celsius?\n");
// Get the input of the temperature
scanf("%f", &celsius);
// Convert to Fahrenheit
float far = (celsius * 9.0 / 5.0) + 32.0;
//Print the temperature in Farenheit.
printf("The temperature converted to Farenheit is %f\n", far);
return 0;
}
This is the output:
What is the temperature in celsius?
5
The temperature converted to Farenheit is 41.000000
Why is it so?
PS - Did I take a right decision by choosing C first? I chose C because I'd heard it makes a good foundation for other languages and also because I like Linux.
In the case
float celsius;
float far = (celsius * 9 / 5) + 32;
you're using celsius uninitialized. It's an automatic storage local scope variable, and unless initialized explicitly, contains indeterminate value.
If not anything, the result will be indeterminate.
However, once you store some value in celsius, and then use it, the proper value will be used and result will be as per expectation.
What else do you expect by the first code.
It's obvious that it will show you some or the other garbage value since the value of celcius is not initialized.
After scanf(), it is initialized so then it works fine.
The other code is absolutely fine.
Kindly use that one
I just started learning C today so this might seem like a simple question but I'm having trouble running my program. I'm trying to implement a program that converts from Celsius to Fahrenheit. The formula for converting from Celsius to Fahrenheit is T(F)=T(C)*9/5+32. The degrees In Celsius is given as 30.
int main()
{
int DegreesCelsius;
printf("30 Degrees Celsius:");
scanf("%d",&30 );
printf("The temperature in F is %d\n", 30 * 9/5 + 32);
return 0;
}
My work is shown above. The problem is on the line containing scanf where I'm told "1value required as unary & operand"
Am I using the ampersand incorrectly?
Thanks
You need to scan the entered value into DegreesCelsius, and then use that variable to perform the conversion.
int DegreesCelsius;
printf("Degrees Celsius: ");
scanf("%d", &DegreesCelsius);
printf("The temperature in F is %d\n", DegreesCelsius * 9/5 + 32);
&30 is invalid syntax, as 30 is an integer literal and cannot be addressed by &; this is why you have DegreesCelsius, to hold the value the user enters.
This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 2 years ago.
I'm working on a program to convert F to C and vice-versa. The thing is, I've seen similar programs where you first enter if the conversion is from degree F to degree C or vice-versa, and then you Enter the data. I just want kind of a more user friendly version, where you Enter the data as "35F" and it gives you output.
So far I have basically %70 of the program done but I think I'm having problems when reading the Temperature and the character together in the same scanf(). I was wondering if it is possible to do that in just 1 line and if I'm aiming in the right directions.
Currently the program is running fine but the Output is always 0.000000 ):
I would love your help and thank your in advance!
float temperature, Farenheit, Celsius;
char unit0;
char unit1 = 'C';
char unit2 = 'F';
printf("Enter the temperature (Ex. 35F): ");
scanf("%f%c", &temperature, &unit0);
if (unit0 == unit2)
{
Celsius = (temperature-32) * (5/9) ;
printf("The temperature %f%c is equal to %f in Celsius", temperature, unit0, Celsius);
}
else
if (unit0 == unit1)
{
Farenheit = (9/5)*temperature+32;
printf("The temperature %f%c is equal to %f in Fahrenheit", temperature, unit0, Fahrenheit);
}
change (5/9) to 5.0/9.0. If you use integer division, this gives you 0 after rounding.
You should do the same for 9/5
I'm new to C language and I'm having trouble about this simple arithmetic operation to convert ounce to metric ton. I don't know how to fix it. It's always giving me wrong result.
#include<math.h>
#define oz 35273.92
main()
{
int ounces;
float mton;
clrscr();
printf("Enter ounces: ");
scanf("%d",&ounces);
mton = ounces/oz;
printf("The metric ton is %f.", mton);
getch();
return(0);
}
I tried entering 70547.84 but the result is wrong.
Enter ounces: 70547.84
The metric ton is 0.014026
If I enter a number lower than oz it gives me -0.000000
Sorry but I can't reproduce this with my compiler (GCC 6.3.0). The result I get is 1.999976, which is fairly reasonable. Also I'm not getting -0.000000 with an input lower than oz.
I suggest, that you should use floating point values for ounces, as you're inputting a decimal number. If you use int you'll not be able to read past the decimal point. You'll get 70547 in ounces, with .84 left in the input stream.
#include <stdio.h>
#define oz 35273.92
int main() {
float ounces;
float mton;
clrscr();
printf("Enter ounces: ");
scanf("%f", &ounces);
mton = ounces/oz;
printf("The metric ton is %f.", mton);
getch();
return(0);
}
This should give you the desired result.
#include<stdio.h>
#include<conio.h>
main()
{
int f,c;
printf("enter the value of celsius in integer (the value of f will be shown in integer neglecting the float value)");
scanf("%d,&c");
f=((9*c)/5)+32;
printf("f=%d,&f");
getch();
}
When i am going to compile and run this program in my window 7 then in compiler it is showing the string to enter the digit but when i am entering a digit to finding out its f then it is giving an error "celcius.exe has stopped working" and after that it is showing "A problem caused the program to stop working correctly. Window will close the program and notify you when a solution will available." How it will be handle in dev c++. Please help me to sorting it out.I am new with c. Thank you.
Change
scanf("%d,&c");
to
scanf("%d",&c);
and
printf("f=%d,&f);
to
printf("f=%d",f);
Side note:
Never use main() instead use int main() and better to use int main(void) and do not forget to add return 0 before closing braces of main.
In Dev C++ no need to use getchar(). It will cause to double press Enter to exit the console.
scanf("%d",&c);
f=((9*c)/5)+32;
printf("f=%d",f);
Change the code as per following
scanf("%d,&c"); to scanf("%d",&c);
printf("f=%d,&f); to printf("f=%d",f);
However your program will give wrong results most of the time. You should declare f and c as float so that f=((9*c)/5)+32; will be evaluated as float division. Now with your code it will be evaluated as integer division. integer division 10/3 will be evaluated to 3 not 3.33.
Rewriting your code
#include<stdio.h>
#include<conio.h>
main()
{
float f,c;
printf("\nEnter Celcius -");
scanf("\n%f",&c);
f=((9*c)/5)+32;
printf("\nf=%f",f);
getch();
return 0;
}