Temperature Converter

Temperature Converter

Medium C Control Flow 28 views
Explanation Complexity

Problem Statement

Take temperature and unit (C/F) input from user. Handle case-insensitive (c, C, f, F). If invalid unit is there then show error and re-input mango. Convert and show result upto 2 decimal places.

Input Format

A temperature value (number) and a unit (C / F, case-insensitive).


Output Format

Converted temperature value printed up to 2 decimal places.

Example

Temperature: 100
Unit: c


Temperature in Fahrenheit = 212.00

Constraints

• Temperature can be an integer or decimal

• Unit must be C, c, F, or f

Concept Explanation

The program converts temperature between Celsius and Fahrenheit based on the unit entered.
Invalid unit input must be handled by showing an error and asking for input again.

Step-by-Step Explanation

1.Ask the user to enter the temperature value.

2.Ask the user to enter the unit (C/F).

3.Convert the unit to a common case (uppercase or lowercase).

4.If the unit is C or c:

• Apply formula:
F = (C × 9/5) + 32

• Print result up to 2 decimal places.

5.Else if the unit is F or f:

• Apply formula:
C = (F − 32) × 5/9

• Print result up to 2 decimal places.

6.Else (invalid unit):

• Show error message "Invalid unit entered"

• Ask the user to re-enter temperature and unit.

7.Repeat until a valid unit is entered.

Concept Explanation

The program converts temperature between Celsius and Fahrenheit based on the unit entered.
Invalid unit input must be handled by showing an error and asking for input again.

Step-by-Step Explanation

1.Ask the user to enter the temperature value.

2.Ask the user to enter the unit (C/F).

3.Convert the unit to a common case (uppercase or lowercase).

4.If the unit is C or c:

• Apply formula:
F = (C × 9/5) + 32

• Print result up to 2 decimal places.

5.Else if the unit is F or f:

• Apply formula:
C = (F − 32) × 5/9

• Print result up to 2 decimal places.

6.Else (invalid unit):

• Show error message "Invalid unit entered"

• Ask the user to re-enter temperature and unit.

7.Repeat until a valid unit is entered.

Input / Output Format

Input Format
A temperature value (number) and a unit (C / F, case-insensitive).


Output Format
Converted temperature value printed up to 2 decimal places.
Constraints
• Temperature can be an integer or decimal

• Unit must be C, c, F, or f

Examples

Input:
Temperature: 100 Unit: c
Output:
Temperature in Fahrenheit = 212.00

Example Solution (Public)

C
#include <stdio.h>

int main() {
    float temp, converted;
    char unit;

    printf("Enter temperature value: ");
    scanf("%f", &temp);

    while (1) {
        printf("Enter unit (C/F): ");
        scanf(" %c", &unit);

        if (unit == 'C' || unit == 'c') {
            converted = (temp * 9 / 5) + 32;
            printf("Temperature in Fahrenheit: %.2f Fn", converted);
            break;
        }
        else if (unit == 'F' || unit == 'f') {
            converted = (temp - 32) * 5 / 9;
            printf("Temperature in Celsius: %.2f Cn", converted);
            break;
        }
        else {
            printf("Invalid unit! Please enter C or F.n");
        }
    }

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    float temp, converted;
    char unit;

    printf("Enter temperature value: ");
    scanf("%f", &temp);

    while (1) {
        printf("Enter unit (C/F): ");
        scanf(" %c", &unit);

        if (unit == 'C' || unit == 'c') {
            converted = (temp * 9 / 5) + 32;
            printf("Temperature in Fahrenheit: %.2f Fn", converted);
            break;
        }
        else if (unit == 'F' || unit == 'f') {
            converted = (temp - 32) * 5 / 9;
            printf("Temperature in Celsius: %.2f Cn", converted);
            break;
        }
        else {
            printf("Invalid unit! Please enter C or F.n");
        }
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.