Convert Celsius to Fahrenheit in C++

This beginner-friendly exercise demonstrates how to perform a temperature conversion from Celsius to Fahrenheit using a basic arithmetic formula in C++. It's an excellent introduction to handling user input, performing floating-point calculations, and displaying output to the console.

You will learn how to work with floating-point variables (`float` or `double`), take input from the user using `cin`, and output the converted result with `cout`. This exercise reinforces the understanding of mathematical expressions and variable usage in a practical context.

Key Concepts:
- Declaring and using floating-point variables
- Accepting input from the user
- Applying a standard mathematical formula
- Outputting formatted results

Group

Introduction to C++

Objective

1. Declare a variable to store the temperature in Celsius.
2. Prompt the user to enter a temperature in Celsius.
3. Use the conversion formula Fahrenheit = (Celsius * 9/5) + 32.
4. Store the result in a second variable.
5. Display the result in Fahrenheit using `cout`.

Convert degrees Celsius to Fahrenheit.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library to handle input and output operations

using namespace std; // Use the standard namespace to avoid prefixing std::

// Main function - entry point of the program
int main() {
    double celsius, fahrenheit; // Declare variables to store Celsius and Fahrenheit values

    // Ask the user to enter temperature in Celsius
    cout << "Enter temperature in Celsius: ";
    cin >> celsius; // Read the Celsius value from user input

    // Apply the formula to convert Celsius to Fahrenheit
    fahrenheit = (celsius * 9 / 5) + 32;

    // Display the result in Fahrenheit
    cout << "Temperature in Fahrenheit: " << fahrenheit << endl;

    return 0; // Return 0 to indicate successful program execution
}

 Output

Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77

Share this C++ Exercise


More C++ Programming Exercises of Introduction to C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Calculate the Area of a Circle in C++

    This exercise helps you practice using variables, mathematical operations, and user input/output in C++ by calculating the area of a circle. The formula to compute the area of a ci...

  • Multiplication Table Generator in C++

    This exercise focuses on generating the multiplication table of a number provided by the user. It introduces the use of loops in C++, specifically the `for` loop, to repeat a set o...

  • Check if a Number is Even or Odd in C++

    This exercise teaches you how to determine whether an integer number is even or odd using conditional statements in C++. It’s a fundamental concept that helps beginners understand ...

  • Currency Converter: Euros to US Dollars in C++

    This exercise is designed to help you understand how to work with floating-point numbers and user input/output in C++. You will build a basic currency converter that takes an amoun...

  • Calculate the Perimeter of a Rectangle in C++

    This exercise focuses on computing the perimeter of a rectangle using basic arithmetic operations in C++. It’s a great way for beginners to get hands-on practice with input/output,...

  • Sum of the First N Natural Numbers in C++

    This exercise introduces the concept of summation using a loop in C++. You will create a program that calculates the sum of the first N natural numbers, where N is provided by the ...