c++ program to convert kelvin to celsius
#include <iostream>
using namespace std;
int main() {
double kelvin, celsius;
cout << "Enter temperature in Kelvin: ";
cin >> kelvin;
celsius = kelvin - 273.15;
cout << "Temperature in Celsius: " << celsius << endl;
return 0;
}
Explanation
The
#include <iostream>
line includes the necessary header file for input and output operations in C++.The
using namespace std;
line allows us to use names from thestd
namespace without specifying the namespace each time. This is done to avoid writingstd::
beforecout
,cin
, etc.The
int main()
function is the entry point of the program. It is where the execution of the program begins.Inside the
main()
function, we declare two variableskelvin
andcelsius
of typedouble
. These variables will be used to store the temperature values.The
cout << "Enter temperature in Kelvin: ";
statement outputs the message asking the user to enter the temperature in Kelvin.The
cin >> kelvin;
statement reads the user input from the standard input and stores it in thekelvin
variable.The
celsius = kelvin - 273.15;
statement calculates the temperature in Celsius by subtracting 273.15 from the Kelvin temperature.The
cout << "Temperature in Celsius: " << celsius << endl;
statement outputs the temperature in Celsius.The
return 0;
statement indicates that the program has completed successfully and returns the value 0 to the operating system.
The program takes the input temperature in Kelvin, subtracts 273.15 to convert it to Celsius, and then outputs the converted temperature in Celsius.