C language not giving me the output in decimals - c

//Converts Farenheit tempretaure to into the celsius scale
#include <stdio.h>
#define FREEZING_PT 32.0f
#define FACTOR 5.0f/9.0f
int main(void)
{
float faren,c;
printf("Enter the Farenheit temperature: ");
scanf("%f",&faren);
float c = (faren - FREEZING_PT)*FACTOR;
printf("The required celsius tempreature is: %.1f\n", c);
return 0;
}
Im a Complete C beginner and this may be very elementary but I cannot figure out the problem here.
In the above code, the value I get returned is always the Celsius temperature in integer values, even if it is a float type. For example, if the Fahrenheit temperature was 0°, the result in Celsius should be -17.7°, but I get the result only as -17°.
Edited code:
//Converts Farenheit tempretaure to into the celsius scale
#include <stdio.h>
#define FREEZING_PT 32.0f
#define FACTOR 5.0f/9.0f
int main(void)
{
float faren,c;
printf("Enter the Farenheit temperature: ");
scanf("%f",&faren);
c = (faren - FREEZING_PT)*FACTOR;
printf("The required celsius tempreature is: %.1f\n", c);
return 0;
}

In your code, you've declared the variable 'c' twice. After removing the first declaration of the variable 'c', it's working properly.
I'm getting the correct output.
Enter the Farenheit temperature: 0
The required celsius tempreature is: -17.8
Enter the Farenheit temperature: 1
The required celsius tempreature is: -17.2
Enter the Farenheit temperature: 2
The required celsius tempreature is: -16.7
Remove the first declaration of the variable 'c'. It should work.

Related

C Convert to Farhenheit Convert to Celsius

I wrote up this quickly and every time I execute the program I always end up with 32 for fahrenheit and 0.00 for celsius and am not sure what the issue is here.
#include <stdio.h>
int main(void)
{
double celsius=0, fahrenheit=0;
printf("Enter a temperature in degrees Celsius: ");
scanf("%f", &celsius);
fahrenheit =(5.0/9.0)*celsius + 32;
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * (5.0/9.0);
printf("That is %.2f Celsius \n", celsius);
return(0);
}
Your format specifier for double is wrong, it should be "%lf". But that's not enough, you should also check that scanf() successfuly read read the value, like this
#include <stdio.h>
int report_error(const char *const message)
{
// TODO: add message formatting capabilities to this function
fprintf(stderr, "error: %s\n", message);
return EXIT_FAILURE;
}
int main(void)
{
double celsius = 0;
double fahrenheit = 0;
printf("Enter a temperature in degrees Celsius: ");
if (scanf("%lf", &celsius) != 1)
return report_error("Invalid Input");
fahrenheit = (5.0 / 9.0) * celsius + 32;
// ^ Define this as a constant?
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
if (scanf("%lf", &fahrenheit) != 1)
return report_error("Invalid Input");
celsius = (fahrenheit - 32) * (5.0 / 9.0);
// ^ see if it was a constant???
printf("That is %.2f Celsius \n", celsius);
return 0;
}
Also, it appears that your algebra went wrong. You should check the celsius expression apparently.

Converting temperatures from Celsius to Fahrenheit

I wrote a program that converts temperatures from Celsius to Fahrenheit. When I enter c, the code works as expected, but when I enter f, it gives me a random number:
#include <stdio.h>
int main (void){
float f,c;
printf("x=Ende c=Umrechnung c->f f=Umrechnung f->c:");
if(scanf("c",&c)==c){
printf("Grad Celisius =");
scanf("%f",&c);
f=(c*1.8)+32;
printf("%.2f Grad Celisius sind %.2f Grad Fahreiheit\n",c,f);
}
else if(scanf("f",&f)==f){
printf("Grad fahrenheit =");
scanf("%f",&f);
c=(f-32)/1.8;
printf("%.2f Grad Fahrenheit sind %.2f Grad Fahrenheit",f,c);
}
return 0;
}
How can I fix my problem?
You didn't use the scanf() command properly. I modified your code a little bit and this works for me:
#include <stdio.h>
int main (void){
float f,c;
char ch;
printf("x=Ende c=Umrechnung c->f f=Umrechnung f->c:");
scanf(" %c", &ch);
if(ch=='c'){
printf("Grad Celsius =");
scanf("%f",&c);
f=(c*1.8)+32;
printf("%.2f Grad Celsius sind %.2f Grad Fahrenheit\n",c,f);
}
else if(ch=='f'){
printf("Grad Fahrenheit =");
scanf("%f",&f);
c=(f-32)/1.8;
printf("%.2f Grad Fahrenheit sind %.2f Grad Celsius",f,c);
}
return 0;
}
Basically, you have read a character (ch) only once and compare it afterwards to the options that your program provides. Please note the space character in " %c" in the scanf() line. It might be needed so that the code works properly, as described here.
I guess that you also want to put a loop around your code, so that it makes sense to ask the user to enter an x in order to end the program. Currently the program terminates if you don't enter c or f. The program also terminates after one calculation has been done.
By the way, I also changed one Celsius to Fahrenheit in your last printf(). I think you confused the words there. I also changed Celisius to Celsius.

Why is my CELSIUS to RANKINE conversion failing when prompting user for a value for CELSIUS?

Perform the following conversions to convert from CELSIUS to RANKINE:
degrees Fahrenheit = (9.0/5.0) * degrees Celsius + 32
degrees Rankine = degrees Fahrenheit + 459.67"
This program converts degrees Celsius to degrees Rankine. Prompt the user for a temperature in Celsius.
#include <stdio.h>
int main(void)
{
double f,c,r;
printf("Enter the temperature in degrees Celsius:" );
scanf("%d", &c);
f = (9.0/5.0) * c +32;
r = f + 459.67;
printf("After your conversion, your temperature in Rankin is: ", r);
return(0);
}
Why is my CELSIUS to RANKINE conversion failing when prompting user for a value for CELSIUS?
Code is using the incorrect type of data per the format specifiers. For type double, scan with "%lf" and to print use "%f"
#BLUEPIXY comment to use the matching format specifiers in sncaf() and printf():
Insure prompt is flushed.
Check scanf() return value.
.
#include <stdio.h>
int main(void) {
double f,c,r;
printf("Enter the temperature in degrees Celsius:" );
fflush(stdout);
if (scanf("%lf", &c) != 1) {
puts("Non-numeric input" );
return -1;
}
f = (9.0/5.0) * c +32;
printf("After your conversion, your temperature in Fahrenheit is: %.1f F", f);
r = f + 459.67;
printf("After your conversion, your temperature in Rankine is: %.1f R", r);
return 0;
}

Printing garbage value while converting farenheit to centigrade using C program

Can anyone help me in writing a c program to convert Celsius to Fahrenheit with only 2 precisions without using any format specifiers.
When I tried using the following code, garbage values are being displayed in the output :
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius,farenheit;
clrscr();
printf("\nEnter the temparature in celsius");
scanf("%f",&celsius);
farenheit=(1.8*celsius)+32.0;
farenheit=farenheit*100;
farenheit=farenheit/100;
printf("The farenheit temcalcparature is:%f",farenheit);
getch();
}
Can anyone help me in completing the code?
You can use rounding functions, but it is better to use format specifiers
#include<stdio.h>
#include<math.h>
void main()
{
float celsius,farenheit;
celsius = 123.12567;
printf("The temparature in celsius:%f\n", celsius);
farenheit=(1.8*celsius)+32.0;
printf("The farenheit temparature is:%f\n",farenheit);
printf("The farenheit temparature is:%0.2f\n",farenheit);
printf("The farenheit temparature is:%d.%d", (int)farenheit, (int)(rint(farenheit*100))%100);
}
Output
The temparature in celsius:123.125671
The farenheit temparature is:253.626205
The farenheit temparature is:253.63
The farenheit temparature is:253.63
farenheit=(1.8*celsius)+32.0;
printf("The farenheit temcalcparature is:%0.2f",farenheit);
will give you what you want

Store user-input in a variable

I was wondering how I could prompt the end-user of my program to type in a value they want to be converted from Fahrenheit into Celsius in C.
Basically, since I'm a total n00b and I'm writing amazing "programs" such as this one:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double celsius, fahrenheit, result;
celsius = result;
fahrenheit = 27;
result = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", result);
return 0;
}
I would like to add some actual "functionality" to it if you know what I mean. Instead of just making this a test program where really it just shows off some simple arithmetic expression evaluating, I would like to actually make it somewhat mildly useful.
Anyway, I was wondering if I could use the function listed in the scanf(3) Man page to aid me in the recognition of user-inputted data, and then somehow store it into the Fahrenheit variable.
Now, it would really be cool if the program, upon running, could prompt the end-user with a question asking whether he or she would like to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius, but let's just take it one step at a time, and I'll wait until I read the chapter in my book about "Making Decisions"! :)
UPDATE:
Removes useless variable result as pointed out by kiamlaluno:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double fahrenheit, celsius;
fahrenheit = 27;
celsius = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", celsius);
return 0;
}
UPDATE UPDATE:
I've been trying to incorporate everyone's helpful suggestions posted here, but I'm running into more problems with my code:
//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit
int main (int argc, char *argv[])
{
int celsius, fahrenheit, celsiusResult, fahrenheitResult;
celsiusResult = (fahrenheit - 32)*(5/9);
fahrenheitResult = (celsius*(9/5)) + 32;
int prompt;
printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%i", &fahrenheit);
printf("%i degress Fahrenheit is %i degrees Celsius!", fahrenheit, celsiusResult);
}
else {
printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
scanf("%i", &celsius);
printf("%i degreses Celsius is %i degrees Fahrenheit", celsius, fahrenheitResult);
}
return 0;
}
Everything's working great, except for the calculations themselves, which come out completely wrong..
For a second I thought this may have been because I changed the numbers themselves to integers types, but I made them doubles again and it was still kind of screwy.
Any thoughts?
To use scanf() to read a double, you'd need to use the correct format string. It would be %lf for "long float" (where %f alone would read into a float). You could then read directly to a double. Same goes for printing it out.
int main (int argc, char *argv[])
{
double fahrenheit, celsius;
printf("farenheit? "); /* write a prompt */
scanf("%lf", &fahrenheit); /* read a double into the fahrenheit variable */
celsius = (fahrenheit - 32) / 1.8;
printf("%lf degress Fahrenheit is %lf degrees Celsius!\n", fahrenheit, celsius);
return 0;
}
Note that this doesn't handle non-numeric inputs at all. You'd need to use other input techniques.
[edit]
You are certainly taking jumps in your code which seems like you're making a lot of progress. :)
To address your most current update, there are a couple of issues. Your code doesn't actually calculate anything, you applied the formula too soon. You should wait until fahrenheit or celsius have meaningful values (i.e., after the user has input the value to be converted). It would be a good idea to move these formulas into functions to perform the conversion. You should also stick with using double and not integers. You will not get the precision you want using integers.
double convert_fahrenheit_to_celsius(double fahrenheit)
{
return (fahrenheit - 32)*(5/9);
}
double convert_celsius_to_fahrenheit(double celsius)
{
return (celsius*(9/5)) + 32;
}
int main (int argc, char *argv[])
{
double celsius, fahrenheit, celsiusResult, fahrenheitResult;
int prompt;
printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%lf", &fahrenheit);
/* now convert user-input fahrenheit to celsius */
celsiusResult = convert_fahrenheit_to_celsius(fahrenheit);
printf("%lf degress Fahrenheit is %lf degrees Celsius!", fahrenheit, celsiusResult);
}
else {
printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
scanf("%lf", &celsius);
/* now convert user-input celsius to fahrenheit */
fahrenheitResult = convert_celsius_to_fahrenheit(celsius);
printf("%lf degreses Celsius is %lf degrees Fahrenheit", celsius, fahrenheitResult);
}
return 0;
}
Ok if you want to prompt the user for input just use scanf with the tag %lf:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double fahrenheit, result;
printf("Please enter a number for farenheit: ");
scanf("%lf", &fahrenheit);
result = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", result);
return 0;
}
As for prompting the user if he/she wants celcius or farenheit you need something called and if statement and else statement.
int main (int argc, char *argv[])
{
double celsius, fahrenheit, result;
int ForC;
printf("1 for Celsius or 0 for Fahrenheit plz: ");
scanf("%d", &ForC);
if(ForC == 1) {
scanf("%lf", &fahrenheit);
result = (fahrenheit - 32) / 1.8;
printf("fahrenheit to celsius:%lf", result);
}
else {
scanf("%lf", &celsius);
result = (celsius*9.0)/5.0 + 32;
printf("celsius to farenheit:%lf", result);
}
return 0;
}
Handling the possibly erroneous user input could be done by checking the return value of scanf(). It must be equal to the number of values expected (1 in this case). In other case, the input should be repeated.
For your UPDATE UPDATE : Try doing the calculation after you get the value from the user. If not, the resulting value of celsius, fahrenheit, celsiusResult, fahrenheitResult is unknown.

Resources