In this program Converts Temperature from Celsius to Fahrenheit.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<iostream> using namespace std; int main() { float cel , far; cout<<"Enter Temperature in Celsius : "; cin>>cel; far=(cel * 9.0) / 5.0 + 32; cout<<"Temperature in Fahrenheit is : " <<far<<endl; return 0; } |
Output:
Here’s an explanation of each line in the code:
1 2 3 |
#include<iostream> |
This line includes the iostream
library, which provides input and output functionality.
1 2 3 |
using namespace std; |
This line specifies that the standard namespace should be used, which makes it possible to use the standard library functions without having to qualify them with the namespace name.
1 2 3 |
int main() |
This line declares the main function, which is the entry point for the program. The int
return type indicates that the function returns an integer value.
1 2 3 |
float cel , far; |
These two lines declare two variables cel
and far
of type float
, which are used to store the temperature in Celsius and Fahrenheit, respectively.
1 2 3 |
cout<<"Enter Temperature in Celsius : "; |
This line uses the cout
stream to output a message prompt to the user asking for the temperature in Celsius.
You may also like: C++ Programs Examples with Explanation
1 2 3 |
cin>>cel; |
This line uses the cin
stream to input a value from the user and store it in the cel
variable.
1 2 3 |
far=(cel * 9.0) / 5.0 + 32; |
This line converts the temperature from Celsius to Fahrenheit using the formula (cel * 9.0) / 5.0 + 32
, and stores the result in the far
variable.
1 2 3 |
cout<<"Temperature in Fahrenheit is : " <<far<<endl; |
This line outputs the converted temperature in Fahrenheit using the cout
stream.
1 2 3 |
return 0; |
This line returns 0 to indicate that the main function has executed successfully.