C++ Program to Convert Celsius to Fahrenheit and Vice-Versa

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

1. Introduction

Temperature conversion is a fundamental concept in physics and everyday life. Converting between Celsius and Fahrenheit is a common task in various applications. In this post, we'll walk through a C++ program that allows the user to convert temperatures between these two scales.

2. Program Overview

Our program will:

1. Display a menu to the user to choose the type of conversion (Celsius to Fahrenheit or Fahrenheit to Celsius).

2. Prompt the user to enter the temperature in the chosen scale.

3. Perform the conversion.

4. Display the converted temperature to the user.

3. Code Program

#include<iostream>
using namespace std;

int main() {
    int choice;
    float temp, convertedTemp;

    cout << "Choose an option:" << endl;
    cout << "1. Convert Celsius to Fahrenheit" << endl;
    cout << "2. Convert Fahrenheit to Celsius" << endl;
    cin >> choice;

    switch(choice) {
        case 1:
            cout << "Enter temperature in Celsius: ";
            cin >> temp;
            convertedTemp = (temp * 9/5) + 32;
            cout << "Temperature in Fahrenheit: " << convertedTemp << endl;
            break;

        case 2:
            cout << "Enter temperature in Fahrenheit: ";
            cin >> temp;
            convertedTemp = (temp - 32) * 5/9;
            cout << "Temperature in Celsius: " << convertedTemp << endl;
            break;

        default:
            cout << "Invalid choice!" << endl;
    }

    return 0;
}

Output:

Choose an option:
1. Convert Celsius to Fahrenheit
2. Convert Fahrenheit to Celsius
1
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77

4. Step By Step Explanation

1. The program displays a menu to the user asking for the type of conversion they'd like to perform.

2. Based on the user's choice, the program then asks for the temperature in the appropriate scale.

3. The conversion is performed using the formulas:

- Celsius to Fahrenheit: (temp * 9/5) + 32- Fahrenheit to Celsius: (temp - 32) * 5/9

4. The converted temperature is then displayed.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare