HOME HTML EDITOR C JAVA PHP

C Functions Challenge:

The best way to learn C is by doing. In this challenge, you will combine everything you've learned—**Return Types**, **Parameters**, and **Function Declarations**—to build a practical utility program.

The Goal: The "Unit Converter" Program

Your task is to create a program that converts temperatures between Celsius and Fahrenheit. This is a classic exercise because it requires mathematical logic and proper function structure.

[Image of Celsius to Fahrenheit conversion formula diagram]

Challenge Requirements

To complete this challenge successfully, your code must meet the following criteria:

Mathematical Formulas

You will need these formulas to write your logic:

---

Stuck? Here is the Solution Template

Try to write it yourself first! If you get stuck, here is a professional structure you can follow:

#include <stdio.h>

// 1. Function Declarations
float toFahrenheit(float celsius);
float toCelsius(float fahrenheit);

int main() {
    float inputTemp = 25.0;

    printf("%.2f Celsius is %.2f Fahrenheit\n", inputTemp, toFahrenheit(inputTemp));
    return 0;
}

// 2. Function Definitions
float toFahrenheit(float celsius) {
    return (celsius * 9 / 5) + 32;
}

float toCelsius(float fahrenheit) {
    return (fahrenheit - 32) * 5 / 9;
}

Bonus Challenges for Pro Coders

If you found the basic challenge too easy, try adding these features:

  1. User Choice: Use a switch or if...else inside main to let the user choose which conversion they want to perform.
  2. Input Validation: Ensure the user enters a valid number.
  3. Recursive Factorial: Add a third function that calculates the factorial of a number using Recursion.
Technical Tip: When performing the math, use 9.0 / 5.0 instead of 9 / 5. In C, 9 / 5 uses integer division and results in 1, whereas 9.0 / 5.0 ensures you get the precise floating-point value of 1.8.