Write a C program for temperature conversion The program sho

Write a C program for temperature conversion. The program should do following: Accept the input temperature value from user Ask for the temperature scale: \'C or \'c\' for centigrade and \'F\' or \'f for Fahrenheit If input for scale is \'C or \'c\' convert the input value into centigrade (assuming input was in Fahrenheit) and print the value in centigrade. If input for scale is \'F\' or \'f convert the input value into Fahrenheit (assuming input was in centigrade) and print the value in Fahrenheit.

Solution

//Program and output given below:

#include <stdio.h>

int main()
{   double t,temp=0; // t for storing user input temperature and temp for converted temperature
    char scale; //scale for storing scale char

    // prompt and accpet input temperature from user
    printf(\"Enter temperature:\");
    scanf(\"%lf\",&t);

    // prompt and accept scale from user
    printf(\"Enter temperature scale(\'C\' or \'c\' for celtigrade or \'F\' or \'f\' for Fahrenheit):\");
    scanf(\"%s\",&scale);

    //if for checking input scale and conversion.
    if(scale==\'C\'||scale==\'c\')
    {
     temp=(t-32)*5/9;
     printf(\"Temperature in Centigrade is %lf \",temp);
    }
    if(scale==\'F\'||scale==\'f\')
    {
     temp=(t*9/5)+32;
     printf(\"Temperature in Fahrenheit is %lf \",temp);
    }
    return 0;
}

/* Output

1)First run                                                                                                                                                         Enter temperature:37                                                                                                                                                     

Enter temperature scale(\'C\' or \'c\' for celtigrade or \'F\' or \'f\' for Fahrenheit):F                                                                                        

Temperature in Fahrenheit is 98.600000                                                                                                     

2)second run:                                                                                                                                     

Enter temperature:98                                                                                                                                                     

Enter temperature scale(\'C\' or \'c\' for celtigrade or \'F\' or \'f\' for Fahrenheit):C                                                                                        

Temperature in Centigrade is 36.666667

 Write a C program for temperature conversion. The program should do following: Accept the input temperature value from user Ask for the temperature scale: \'C

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site