Table of Contents
How to write a program to convert Celsius to Fahrenheit in C++
In this article, we will discuss the concept of How to write a program to convert Celsius to Fahrenheit in C++
In this post, we will learn how to write a program to Convert Celsius into Fahrenheit and display the result on the screen in C++ programming language.
Code to Celsius into Fahrenheit
Convert Celsius into Fahrenheit -#1
In this program, the user declares two variables as Celsius and Fahrenheit, and initiates the value as 36.9 to variable- Celsius. It will be converted from Celsius into Fahrenheit in the C programming language using the scientific equation.
Program 1
//C++ code to convert Celsius into Fehrenheit
#include <iostream>
using namespace std;
int main() {
float celsius, fahrenheit;
//declare float type variables
//initialize variable
celsius=36.9;
//Calculate fahrenheit from celsius
fahrenheit=(celsius * 9/5)+32;
cout<<celsius<<" Celsius is same as: "<<fahrenheit <<" Fahrenheit";
//display result on the screen
return 0;
}
When the above code is executed, it produces the following result
36.9 Celsius is the same as: 98.42 Fahrenheit
Convert Celsius into Fahrenheit -Entered by the user-#2
In this program, the user declares two variables as Celsius and Fahrenheit. The program asks for input from the user for Celsius then the given input will convert from Celsius into Fahrenheit in the C++ programming language using the scientific equation.
Program 2
//C++ program to convert C into f from user input
#include <iostream>
using namespace std;
int main() {
float celsius, fahrenheit;
//declare float type variables
// ask input from the user for cesius
cout<<"Enter the temperature in Celsius:";
cin>>celsius;//reading the input
//Calculate fahrenheit from given celsius value
fahrenheit=(celsius * 9/5)+32;
cout<<celsius<<" Celsius is the same as: "<<fahrenheit <<" Fahrenheit";
//display result on the screen
return 0;
}
When the above code is executed, it produces the following result
Enter the temperature in Celsius:99.99 99.99 Celsius is the same as: 211.982 Fahrenheit
Convert Celsius into Fahrenheit -Using function-#3
In this program, the user declares a double variable as a parameter for Celsius in the user-defined function. When the user runs the program (when calling the function), the value for Celsius passes as an argument to the function.
Finally, the Fahrenheit value is calculated and displayed on the screen using the scientific equation.
Program 3
//C++ program to convert C into F using function
#include <iostream>
using namespace std;
float CelsiusToFahrenheit(float);
//function prototype
int main() {
float celsius;
//declare float type variables
//Calculate Fahrenheit
float fahrenheit=CelsiusToFahrenheit(32.00);
float fahrenheit1=CelsiusToFahrenheit(99.00);
float fahrenheit2=CelsiusToFahrenheit(105.50);
float fahrenheit3=CelsiusToFahrenheit(37.00);
//Calling the function with an argument
//display result on the screen
cout<<fahrenheit <<" Fahrenheit"<<endl;
cout<<fahrenheit1 <<" Fahrenheit"<<endl;
cout<<fahrenheit2 <<" Fahrenheit"<<endl;
cout<<fahrenheit3 <<" Fahrenheit"<<endl;
return 0;
}
//function definition
float CelsiusToFahrenheit(float celsius){
return (celsius * 9/5)+32;
}
When the above code is executed, it produces the following result
89.6 Fahrenheit 210.2 Fahrenheit 221.9 Fahrenheit 98.6 Fahrenheit
Convert Celsius into Fahrenheit Using the function-Entered by the user-#4
In this program, the user declares a double variable as a parameter for Celsius in the user-defined function. When the user runs the program (when calling the function), The program asks for input from the user for Celsius and the value for Celsius passes as an argument to the function.
Finally, the Fahrenheit value is calculated and displayed on the screen using the scientific equation.
Program 4
//C++ program to convert C into F using function
//with user input
#include <iostream>
using namespace std;
float CelsiusToFahrenheit(float);
int main() {
float celsius;
//declare float type variables
// ask input from the user
cout<<"Enter the temperature in Celsius:";
cin>>celsius;//reading the input
//Calculate Fahrenheit
float fahrenheit=CelsiusToFahrenheit(celsius);
//Calling the function with an argument
//display result on the screen
cout<<celsius<<" celsius"<<" equal to "<<fahrenheit <<" Fahrenheit"<<endl;
return 0;
}
//function definition
float CelsiusToFahrenheit(float celsius){
return (celsius * 9/5)+32;
}
When the above code is executed, it produces the following result
Enter the temperature in celsius:111.11 111.11 Celsius equal to 231.998 Fahrenheit
Similar post
Celsius into Fahrenheit in the C program
Fahrenheit into Celsius in C program
Celsius into Fahrenheit in C++ program
Fahrenheit into Celsius in C++ program