Program for Celsius to Fahrenheit Conversion and Vice Versa

The problem statement here asks us to write a program in which a user provides input in celsius. Then we need to convert the temperature to fahrenheit and print it and the opposite …

3 mins
Program for Celsius to Fahrenheit Conversion and Vice Versa

The problem statement here asks us to write a program in which a user provides input in celsius. Then we need to convert the temperature to fahrenheit and print it and the opposite from fahrenheit to celsius.

Example:

Enter temperature in celsius: 0
Temperature in fahrenheit is: 32.000000

Enter temperature in fahrenheit: 32
Temperature in celsius is: 0.000000

Here, we will use the formula [(celsius * 9 / 5) + 32] to convert the temperature from celsius to fahrenheit and the formula [(fahrenheit - 32) * 5 / 9] will be used to convert the temperature from fahrenheit to celsius.

Implementation in Codes

C

#include <stdio.h>
void main()
{
    float celsius, fahrenheit;
    // For Celsius to Fahrenheit
    printf("Enter temperature in celsius: ");
    scanf("%f", &celsius);
    printf("Temperature in fahrenheit is: %f\n", celsius * 9 / 5 + 32);

    // For Fahrenheit to Celsius
    printf("\nEnter temperature in fahrenheit: ");
    scanf("%f", &fahrenheit);
    printf("Temperature in celsius is: %f\n", (fahrenheit - 32) * 5 / 9);
}

Output:

Enter temperature in celsius: 37
Temperature in fahrenheit is: 98.599998

Enter temperature in fahrenheit: 98.6
Temperature in celsius is: 37.000000

C++

#include <iostream>
using namespace std;
int main()
{
    float celsius, fahrenheit;
    // For Celsius to Fahrenheit
    cout << "Enter temperature in celsius: ";
    cin >> celsius;
    cout << "Temperature in fahrenheit is: " << (celsius * 9 / 5 + 32) << endl;

    // For Fahrenheit to Celsius
    cout << "\nEnter temperature in fahrenheit: ";
    cin >> fahrenheit;
    cout << "Temperature in celsius is: " << (fahrenheit - 32) * 5 / 9 << endl;
    return 0;
}

Output:

Enter temperature in celsius: 37
Temperature in fahrenheit is: 98.6

Enter temperature in fahrenheit: 98.6
Temperature in celsius is: 37

Python

# take user input in celsius
celsius = input("Enter temperature in celsius: ")
# convert celsius to fahrenheit
fahrenheit = float(celsius) * (9 / 5) + 32
# print the converted value
print(f"Temperature in fahrenheit: {fahrenheit}")

# take input in fahrenheit
fahrenheit = input("\nEnter temperature in fahrenheit: ")
# convert fahrenheit to celsius
celsius = (float(fahrenheit) - 32) * 5 / 9
# print the converted value
print(f"Temperature in celsius: {celsius}")

Output:

Enter temperature in celsius: 37
Temperature in fahrenheit: 98.60000000000001

Enter temperature in fahrenheit: 98.6
Temperature in celsius: 37.0

JavaScript

// take user input in celsius
let celsius = 36.664
// convert celsius to fahrenheit
fahrenheit = celsius * (9 / 5) + 32
// print the converted value
console.log(`Temperature in fahrenheit: ${fahrenheit}`)

// take input in fahrenheit
fahrenheit = 98.68989
// convert fahrenheit to celsius
celsius = (fahrenheit - 32) * 5 / 9
// print the converted value
console.log(`Temperature in celsius: ${celsius}`)

Output:

Temperature in fahrenheit: 97.99520000000001
Temperature in celsius: 37.04993888888889

So, these were the codes for conversion of temperature from celsius to fahrenheit and vice versa. Try out building the logic on your own and then converting the logic into codes. Also, see how to set up VSCode for Python programming and C/C++ programming. I hope the article helped you learn something new, drop a like if it did.

Was this Article Helpful?
Ajay Choudhury

Ajay Choudhury

A learner and a student of technlogy. He loves sharing experiences and learning with others through his projects and blog. Along with technology, he loves playing football and enjoys listening to music and podcasts.

0 Comments

Info

OrbitGadget — A constructive and and resourceful hub for computer science geeks. Let us achieve together.

Privacy Policy Disclosure Site Terms Atom Feed Sitemap

Made with ❤️ and Django. OrbitGadget © 2020 - 2022.