Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# C program help

### #1 bluntknife Posted 30 April 2007 - 09:22 AM

bluntknife

Member

• Member
• 55 posts
hi, i have created a temp converter which will convert from fahrenhiet to celsius and vice versa. it is menu driven but for some reason it wont convert from celsius to fahreenheit but everything else works

any help would be a precaded

here is the code for the celsius to fahrenheit conversion;

printf("***************************************************************\n");
printf(" >> Celsius to Fahrenhiet converter <<\n\n");
printf(" >> Enter temperature in Celsius = ");
scanf("%d", &ce);
fa=(ce*9/5)+32;
printf(" >> The tempurature in Fahrenhiet is %d\n", ce);
printf("***************************************************************\n");
break;

just say if you need more info

many thanks
• 0

### #2 bobmad Posted 30 April 2007 - 09:50 AM

Member

• Member
• 344 posts
The answer to: x = 3 / 2 is 1 even if x is declared a float!!

(ce* 9 / 5 ) is not giving you what you are expecting...
• 0

### #3 stettybet0 Posted 30 April 2007 - 05:17 PM

stettybet0

Trusted Tech

• Technician
• 2,579 posts
Here's code giving you what you want. I included plenty of comments so that you (hopefully) will understand it. If you don't understand something, please ask about it! If you just copy this, but don't understand it, then you're not learning anything and it will hurt you in the long run.

```#include <stdio.h>

int main()
{
float fa, ce;
/* Declare your variables as floating variables.
* The reason being that you will want decimal answers.
*/
printf("***************************************************************\n");
printf(" >> Celsius to Fahrenheit converter <<\n\n");
/* Fahrenheit is spelled Fahrenheit, not Fahrenhiet. =)
*/
printf(" >> Enter temperature in Celsius = ");
scanf("%f", &ce);
/* "%f" has to be used instead of "%d".
* "%f" is for floating numbers, "%d" is for integers.
*/
fa=(ce*(float)9/5)+32;
/* When you want a decimal answer to the division of two integers...
* Declare the first integer as floating!
*/
printf(" >> The temperature in Fahrenheit is %f\n", fa);
/* Print the temperature (not tempurature - your typo) in Fahrenheit.
* You had it printing the original Celsius temperature.
*/
printf("***************************************************************\n");
return 0;
/* Use return 0
* Unless the code you gave was part of a loop or something...
*/
}```

-stettybet0

Edited by stettybet0, 30 April 2007 - 05:17 PM.

• 0

### #4 bluntknife Posted 01 May 2007 - 10:07 AM

bluntknife

Member

• Topic Starter
• Member
• 55 posts
ok thanks i have got in working now, there was a small typo in the code. instead of useing the correct fa i used ce.

many thanks

ps i have converted the whole program so that it use float bt i was wonding if you knew how limit the out come to teo decimal points?

Edited by bluntknife, 01 May 2007 - 10:17 AM.

• 0

### #5 stettybet0 Posted 01 May 2007 - 12:56 PM

stettybet0

Trusted Tech

• Technician
• 2,579 posts
If by "teo" you mean two, then:

`printf(" >> The temperature in Fahrenheit is %0.2f\n", fa);`

If by "teo" you mean ten, then:

`printf(" >> The temperature in Fahrenheit is %0.10f\n", fa);`

-stettybet0
• 0

### #6 bluntknife Posted 02 May 2007 - 03:58 AM

bluntknife

Member

• Topic Starter
• Member
• 55 posts
ok, sorry i did mean two but i was rushed when i write the reply.

thanks for your help stettybet0
• 0

### #7 stettybet0 Posted 02 May 2007 - 12:44 PM

stettybet0

Trusted Tech

• Technician
• 2,579 posts
Heh, it's okay.

I'm glad I could help.
• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users