Monday, October 19, 2015

Program with Function

*Example of a program with functions*/
/*temperature conversion program*/

#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
double CelToFahr(double temp_cel);
double FahrToCel(double fahr_temp);

int main()
{
   int answer_1, dummy;
   double celsius, fahr;
   
   answer_1=TRUE;

  
   while(answer_1==TRUE) {
         printf("\nInput degrees Fahrenheit for conversion to Celsius(^z to exit):  \n");
      scanf("%lf",&fahr);
      celsius=FahrToCel(fahr);
      printf("\n%lf Celsius.\n\n",celsius);
     
      printf("\nAnother F to C conversion?  (0 to exit conversion):  \n");
      scanf("%d", &answer_1);
     }
   printf("\nDo you want to input degrees in Celsius for conversion? (yes=1)\n");
   scanf("%d",&answer_1);
 
      
       while(answer_1==TRUE) {
        printf("\n\nInput degrees Celsius for conversion to Fahrenheit (^z to exit):  \n");
        scanf("\n%lf",&celsius);
        fahr=CelToFahr(celsius);
          printf("\n%lf Fahrenheit.\n\n", fahr);
         
        }
   
   printf("End of program - enter any number to exit.\n");
   scanf("%d",&dummy);
}

double FahrToCel(double fahr_temp)
/* Function to convert Fahrenheit to Celsius */
{
   double cel_temp;
   cel_temp=(5.0/9.0)*(fahr_temp-32.0);
   return(cel_temp);
}

double CelToFahr(double temp_cel)
/* Function to convert Celsius to Fahrenheit */
{
   double temp_fahr;
   temp_fahr=(9.0/5.0)*temp_cel+32.0;;
   return(temp_fahr);
}

No comments:

Post a Comment