5. Program that gets the number and a letter. If the letter is 'f', convert it to the temperature in degree Celsius. If the letter is 'c', convert it to Fahrenheit temperature.

Chapter 5: 

Conditional Structures

Programming Exercise

Problem # 5:

Write a program that gets the number and a letter. If the letter is 'f', the program should treat the number entered as temperature in degrees Fahrenheit and convert it to the temperature in degree Celsius and print a suitable message. If the letter is 'c', the program should consider the number as Celsius temperature and convert it to Fahrenheit temperature and print a suitable message. The program should display error message and then exit if the user enters any other letter.

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
    float n,C,F;
    char c;
    cout<<"Enter a number:";
    cin>>n;
    cout<<"\nEnter a letter:";
    cin>>c;
    if(c=='f')
    {
        C=(n-32)*5/9;
        cout<<"\nthe temperature after converting into degree celsius is:"<<C;
    }
    else if(c=='c')
    {
        F=9/5*n+32;
        cout<<"\nThe temperature after converting into degree fahrenheit is:"<<F;
    }
    else
    cout<<"\nerror.";
    return 0;
}


Let me know in the comment section if you have any question.

Previous Post:
4. Program that contains an if statement that may be used to compute the area of a square or a triangle after prompting the user to type the first character of the figure names.

Comments