Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Develop a program that converts temperatures between Celsius and Fahrenheit. Prompt the user for input and provide options for conversion.

#include <stdio.h>

int main() {
// Declare variables
float temperature, convertedTemperature;
char choice;

// Input: Get the temperature and conversion choice from the user
printf(“Enter the temperature: “);
scanf(“%f”, &temperature);

printf(“Choose conversion type:n”);
printf(“C – Celsius to Fahrenheitn”);
printf(“F – Fahrenheit to Celsiusn”);
printf(“Enter your choice (C/F): “);
scanf(” %c”, &choice); // Note the space before %c to consume any previous newline character

// Process: Perform the temperature conversion
switch (choice) {
case ‘C’:
case ‘c’:
convertedTemperature = (temperature * 9 / 5) + 32;
printf(“%.2f Celsius is %.2f Fahrenheit.n”, temperature, convertedTemperature);
break;
case ‘F’:
case ‘f’:
convertedTemperature = (temperature – 32) * 5 / 9;
printf(“%.2f Fahrenheit is %.2f Celsius.n”, temperature, convertedTemperature);
break;
default:
printf(“Error: Invalid choice entered.n”);
return 1; // Exit the program with an error code
}

return 0; // Exit the program successfully
}