In this C programming example we will learn how to convert temperature from Celsius to Fahrenheit degrees.
Before write the program first we need to know the relation between Celsius and Fahrenheit.
The Mathematical formula is
Fahrenheit=Celsius +32 |
So now based on this formula we will write c program to convert temperature
In this Example we will Convert between given user input values start and end and the step number to increment the count.
take Celsius value which we need to take from user input and highest value upto which we need to convert
#include <stdio.h> #define LOWER_LIMIT 0 #define HIGHER_LIMIT 1000 int main(void) { double fahr, cel; int limit_low = -1; int limit_high = -1; int step = -1; int max_step_size = 0; /* Read in lower, higher limit and step */ while(limit_low < (int) LOWER_LIMIT) { printf("Please enter a lower limit, limit >= %d: ", (int) LOWER_LIMIT); scanf("%d", &limit_low); } while((limit_high <= limit_low) || (limit_high > (int) HIGHER_LIMIT)) { printf("Please enter a higher limit, %d < limit <= %d: ", limit_low, (int) HIGHER_LIMIT); scanf("%d", &limit_high); } max_step_size = limit_high - limit_low; while((step <= 0) || (step > max_step_size)) { printf("Please enter a increment step, 0 < step >= %d: ", max_step_size); scanf("%d", &step); } /* Initialise Celsius-Variable */ cel = limit_low; /* Print the Table */ printf("\nCelsius\t\tFahrenheit"); printf("\n-------\t\t----------\n"); while(cel <= limit_high) { fahr = (9.0 * cel) / 5.0 + 32.0; printf("%f\t%f\n", cel, fahr); cel += step; } printf("\n"); return 0; } |
Output
Please enter a lower limit, limit >= 0: 50 |
Conclusion: In this C program we learned how to convert temperature from Celsius to Fahrenheit