C Programming - A Program to Convert Celsius Temperatures to Fahrenheit

 


Program Logic

This program converts the Celsius temperature entered by the user to Fahrenheit and displays the result with two decimal places. It then asks the user if they want to convert another temperature or quit. If the user chooses to convert another temperature, the loop continues, and if they choose to quit, the program exits.


C Source Code


#include <stdio.h> int main() { float celsius, fahrenheit; char choice; do { printf("\nEnter temperature in Celsius: "); scanf("%f", &celsius); fahrenheit = (celsius * 1.8) + 32; printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit); printf("\nDo you want to convert another temperature (y/n)? "); scanf(" %c", &choice); } while (choice == 'y' || choice == 'Y'); return 0; }

 

C Source Code Explained

The program first includes the stdio.h header file which contains functions for input and output operations.


In the main() function, we declare three variables: celsius and fahrenheit as floating-point numbers and choice as a character.


Then, we start a do-while loop that asks the user to enter a temperature in Celsius using the printf() function to display a prompt message and the scanf() function to read the input value from the user.


Next, we convert the temperature from Celsius to Fahrenheit using the formula F = C * 1.8 + 32 and store the result in the fahrenheit variable.


Then, we use the printf() function to display the converted temperature in Fahrenheit to the user with two decimal places.


Finally, we ask the user if they want to convert another temperature using the printf() function to display a prompt message and the scanf() function to read the input value from the user.


If the user enters 'y' or 'Y', the loop continues, and if they enter any other character, the loop exits.


Sample Program Run

Here's a sample run of the program:


Enter temperature in Celsius: 25 25.00 Celsius = 77.00 Fahrenheit Do you want to convert another temperature (y/n)? y Enter temperature in Celsius: 0 0.00 Celsius = 32.00 Fahrenheit Do you want to convert another temperature (y/n)? n

 


Image by 200 Degrees from Pixabay 


Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process