Convert Celsius to Fahrenheit

function celsius to fahrenheit

Page 1 of 1

4 Replies - 8397 Views - Last Post: 03 March 2009 - 07:44 PM Rate Topic: -----

#1 pun_indo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 28-February 09

Convert Celsius to Fahrenheit

Posted 02 March 2009 - 07:38 PM

hi, please help me figure out how can I solve this problem. I am having problems in writing this code.

The question that asks is that :

a) Function celsius returns the Celsius equivalent of a Fahrenheit temperature.

I did that but I think there is some problems, so maybe someone here can help me out

my code

#include <stdio.h>

int temperature( int c, int f)
{ 
	if (c==0)
		return c;
	else
		return f;
}
int main(void)

{
	int celsius, fahrenheit, temp;

	printf("Enter temp");
	scanf("%d", &fahrenheit);

	printf("Enter temp");
	scanf("%d", &celsius);

	temp=1.8*celsius+32;

	printf("temp = %d", temp);

return 0;

}




please help me. tell me what I did wrong.

thanks

Is This A Good Question/Topic? 0
  • +

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Convert Celsius to Fahrenheit

Posted 02 March 2009 - 10:49 PM

Your formula for the Celsius to Fahrenheit should be IN the function, not in main. Because it is not in the function, what you have there makes really no sense.

float celsiusToFahrenheit(float celsiusTemp)
{
    return ((9.0/5.0) * celsiusTemp) + 32;
}



Notice here how we put the formula inside the temperature conversion function and return the answer using the return statement.

Then from main we can call this function and get the answer...

int main() {
     // Call the function and have it return the converted value
     float Fahrenheit = celsiusToFahrenheit(23);

     printf("After conversion: %.2f",Fahrenheit);
     return 0;
}



So this should give you the idea of how a function works and how we could use it to do some work for us and get a result from it.

Enjoy!

"At DIC we be temperature converting code ninjas... DIC is in the forecast, expect extreme blizzard conditions with a temperature of absolute zero. Oh yes, we are that cool!" :snap:
Was This Post Helpful? 1

#4 pun_indo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 28-February 09

Re: Convert Celsius to Fahrenheit

Posted 03 March 2009 - 06:46 PM

Thanks a lot sir. I appreciate that . I will try to do more on it and will ask questions if I have any

but can you explain me that why did u put
float Fahrenheit = celsiusToFahrenheit(23);
I did not get it . what does 23 has to do ?

Thanks again

This post has been edited by pun_indo: 03 March 2009 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Convert Celsius to Fahrenheit

Posted 03 March 2009 - 07:12 PM

23 is the celsius temperature he is telling the function to convert.
Was This Post Helpful? 0
  • +
  • -

#6 pun_indo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 28-February 09

Re: Convert Celsius to Fahrenheit

Posted 03 March 2009 - 07:44 PM

ya got it after I played around with the code :D

thanks though .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1