C - Write program to allocate memory for float type and do calculation

Requirements

P:Read the current temperature outside as a floating-point value.

Have the code ask whether the input is Celsius or Fahrenheit.

Use malloc() to create storage for the value input.

Display the resulting temperature in Kelvin.

Hint

Here are the function to calculate Kelvin:

kelvin = celsius + 273.15; 

kelvin = (fahrenheit + 459.67) * (5.0/9.0); 

Demo

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()/*from   w w w.ja  v a2s  .c o  m*/
{
    float *temperature;
    char c;

    temperature = (float *)malloc(sizeof(float)*1);
    if(temperature == NULL)
    {
        puts("Unable to allocate memory");
        exit(1);
    }
    printf("What is the temperature? ");
    scanf("%f",temperature);
    getchar();
    printf("Is that Celsius or Fahrenheit (C/F)? ");
    c = toupper(getchar());
    if(c=='F')
        *temperature=(*temperature+459.67)*(5.0/9.0);
    else
        *temperature+=273.15;
    printf("It's %.1f Kelvin outside.\n",*temperature);

    return(0);
}

Result

Related Exercise