Declare it GLOBAL

Loading...

Search! HigherThanGetche!

Saturday, September 5, 2009

C: SWITCH and PASSING VALUES: Celsius to Fahrenheit converter

For this entry I would be sharing a Celsius to Fahrenheit converter. This program utilizes the following statements: IF, SWITCH and CASE.

This program also passes values to an existing function (outside main)

I hope these codes would help any student or aspiring programmer. I believe that C is the most basic of all programming languages - you can't learn other languages unless you understand the logic and flow of C.


#include <stdio.h>

convert(int a, float b)
{
float far, cel;

switch(a)
{
case 1:
far=(9*b/5)+32;
printf( "%f degrees Celcius is equivalent to %f degrees Fahrenheit",b,far);
break;
case 2:
cel=5*(b-32)/9;
printf( "%f degrees Fahrenheit is equivalent to %f degrees Celsius",b,cel);
break;
default:
printf("invalid input!");
}
getche();

}

main()
{
float temperature;
int choice;

printf("What conversion do you desire?\n 1-Celsius to Fahrenheit\n -or-\n 2-Fahrenheit to Celsius\n");
scanf( "%d", &choice);
printf("Input the temperature you would like to convert:\n");
scanf("%f",&temperature);
if(choice==1 || choice==2)
convert(choice, temperature);

}

0 comments:

Post a Comment