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
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:
Image by 200 Degrees from Pixabay
Comments
Post a Comment