PROBLEM #2: Temperature Unit Converter

4 0 0
                                        

            ## DESCRIPTION ##
/*
PROBLEM #2

Create a program that convert a temperature from from any  temperature measuring unit
Example:
    1.    CELSIUS
    2.    FAHRENHEIT
    3.    KELVIN
    Enter your choice: 3
   
   
   
    Enter the temperature in kelvin: 40
   
   
   
    1.    CELSIUS
    2.    FAHRENHEIT
    Enter the unit measure to transform 40°K: 1
   
 
    40°K =  313.15°C
   
*/

***

#include <stdio.h>

double convert(int, int, double);
char symbol(int, int);

int main(){
    int choice1, choice2;
    int count = 0, i;
    double temperature;
    char name[3][20] = {"Fahrenheit", "Celsius", "Kelvin"};
   
  
    printf("\t\t***TEMPERATURE CONVERTER***

");
  
    for(i = 0; i < 3; i++){
        printf("%d. %s

", i + 1, name[i]);
    }
   
    printf("

Enter your choice: ");
    scanf("%d", &choice1);
   
    printf("

Enter the temperature in %s", name[choice1 - 1]);
    scanf("%lf", &temperature);
   
    printf("

");
   
    for(i = 0; i < 3; i++){
        if(i != (choice1 - 1)){
            count++;
            printf("%d. %s

", count, name[i]);
        }
    }
   
    printf("

Convert %0.4lf°%c to: ", temperature, name[choice1-1][0]); //Get the first letter and the temperature measure
    scanf("%d", &choice2);
   
  
   
    printf("

CONVERSION:

\t\t%0.4lf°%c = %0.4lf°%c

", temperature, name[choice1 - 1][0], convert(choice1, choice2, temperature), symbol(choice1, choice2));

    return 0;
}

double convert(int choice1, int choice2, double temperature){
      
    if(choice1 == 1 && choice2 == 1)
        return (temperature - 32) * 0.5556;
  
    if(choice1 == 1 && choice2 == 2)
        return ((temperature -32) * 0.5556) + 273.15;
      
    if(choice1 == 2 && choice2 == 1)
        return (temperature * 1.8) + 32;
     
    if(choice1 == 2 && choice2 == 2)
        return temperature + 273.15;       
   
    if(choice1 == 3 && choice2 == 1)
        return 1.8 * (temperature - 273.15) + 32;
     
    if(choice1 == 3 && choice2 == 2)
        return temperature - 273.15;
}

char symbol(int choice1, int choice2){
   
    if(choice1 == 1 && choice2 == 1)
        return 'C';
     
    if(choice1 == 1 && choice2 == 2)
        return 'K';   
   
    if(choice1 == 2 && choice2 == 1)
        return 'F';  
 
    if(choice1 == 2 && choice2 == 2)
        return 'K';
   
    if(choice1 == 3 && choice2 == 1)
        return 'F';
   
    if(choice1 == 3 && choice2 == 2)
        return 'C';
   
}

***

You've reached the end of published parts.

⏰ Last updated: Jun 23, 2023 ⏰

Add this story to your Library to get notified about new parts!

Programming in C/C++ Where stories live. Discover now