Temperature Converter using C Language

 In this  I will tell you how to make temperature Converter using C language . So, without wasting   our  time let's get started with our today's  topic .  Let us first see the code then we understand logic and then    output.

Code:

#include <stdio.h>
int main()
{
    float inputTemp;
    char inputType;
    char outputType;
    float outputTemp;
    printf("Enter the unit for input (k for Kelvin, F for Fahernite ,C for Celcius)\n");
    scanf(" %c", &inputType); 
    printf("Enter the value for input \n");
    scanf("%f", &inputTemp);
    printf("Enter the unit for output (k for Kelvin, F for Fahernite ,C for Celcius)\n");
    scanf(" %c", &outputType);                //for output value
    if (inputType == 'c' || inputType == 'C'//when input type is Celsius
    {
        if (outputType == 'f' || outputType == 'F')
        {
            outputTemp=(inputTemp *9/5)+32;
            printf("Temperature in Fahernite : %.2f ", outputTemp);
        }
        else if (outputType == 'K' || outputType == 'k')
        {
            outputTemp = inputTemp + 273.15// from Celcius to Kelvin
            printf("Temperature in Kelvin : %.2f", outputTemp);
        }
    }
    if (inputType == 'f' || inputType == 'F')
    { //when input type is Fahernite
        if (outputType == 'C' || outputType == 'c')
        {
            outputTemp = (inputTemp - 32) * 5 / 9
            printf("Temperature in Celcius: %.2f", outputTemp);
        }
        else if (outputType == 'K' || outputType == 'k')
        {
            outputTemp = (inputTemp - 32) * 5 / 9 + 273.15
            printf("Temperature in Kelvin: %.2f", outputTemp);
        }
    }
    if (inputType == 'K' || inputType == 'k')
    {  //when input is in Kelvin 
        if (outputType == 'F' || outputType == 'f')
        {
            outputTemp = (inputTemp - 273.15) * 9 / 5 + 32;
            printf("Temperature in Fahernite : %.2f", outputTemp);
        }
        else if (outputType == 'C' || outputType == 'c')
        {
            outputTemp = inputTemp - 273.15;
            printf("Temperature in Celcius: %.2f", outputTemp);
        }
    }
    return 0;
}

Logic:

Here we first define two variable of type float to take input Temperature and give output Temperature that is inputTemp and outputTemp. And then we define two variable of type Char to take Character of unit in which we want to give input to program(inputType) and then for output we use (outputType). And after taking these values we first check make all case that can be possible if input in Celsius  and in outputTemp variable we assign formula that we need to convert Celsius  into Fahrenheit and if user want output in Kelvin then we use formula to Celsius into Kelvin. And we did same if  input is in Kelvin  or in Fahrenheit . And alter only formula and done with our work . You can check for formula on internet . 

Output:




      now here you can see that our program work great . As we give 0 input it give output as we desired           in another unit .

     I hope this will help you in your work .

     Thanks for reading .


No comments:

Post a Comment