# CS201 Introduction to Programming Assignment 1 Solution Fall 2013

Objective:

The objective of this assignment is to provide hands on experience of:

• Basic concepts of C/C++ language and Programming
• Dealing with Data types
• Repetition Structures (Loops)
• Switch statement
• Saving a program
• Compiling a program
• Executing the program

Guidelines:

• Code should be properly indented and well commented.
• Follow C/C++ rules while writing variable names, function names etc
• Use only dev-C++ for this assignment.

Assignment

Problem Statement:

You are required to write a simple “Temperature Conversion Calculator” using C++ language. The objective of this program should be to convert one temperature unit to other temperature units. These temperature units are Fahrenheit, Celsius and Kelvin etc.

Detailed Description:

The program should prompt the user to enter his/her option.

The program should respond in the following ways using switch statement:

1. If user enters option ‘F’ or ‘f’ then it should prompt the user to enter temperature in Fahrenheit. It should then convert it into Celsius and Kelvin and display the values on the screen.
2. If user enters option ‘C’ or ‘c’ then it should prompt the user to enter temperature in Celsius. It should then convert it into Fahrenheit and Kelvin and display the values on the screen.
3. If user enters option ‘K’ or ‘k’ then it should prompt the user to enter temperature in Kelvin. It should then convert it into Celsius and Fahrenheit and display the values on the screen.

After performing conversion operation, it should ask the user to continue or not. If user enters ‘n’ then it will quit the program otherwise it will perform the operation again

Sample output of program:

When user enters option C or c then it convert into Fahrenheit and Kelvin

When user enters option F or f then it convert into Celsius and Kelvin

When user enters option K or k then it convert into Celsius and Fahrenheit

Hint:

Use the formulas given below for conversion.

K = °C + 273.15

°F = °C × 9⁄5 + 32

K  = (°F + 459.67) × 5⁄9

Solution:

[box type=”info”]

#include <iostream.h>

main()

{

double f_temp, k_temp, c_temp, temp;

char ch, quit;

cout << ” *********** Temprature Conversion Calculator **************”;

cout << “\n\n Please enter the temprature unit for which you want the coverison “;

cout << “\n 1. F for Fahrenheit to Celcius and Kalvin”;

cout << “\n 2. C for Celsius to Fahrenheit and Kalvin”;

cout << “\n 3. K for Kalvin to Fahrenheit and Celcius”;

startagain:

cout << “\n\n Please enter you choice: “;

cin >> ch;

switch(ch)

{

case ‘f’:

case ‘F’:

cout << ” Please enter temprature in Farhenheit: “;

cin >> f_temp;

c_temp = (f_temp  –  32)  *  5/9;

k_temp = (f_temp + 459.67) * 5/9;

cout << ” Celcius =” << c_temp;

cout << “\n Kelvin =” << k_temp;

cout << “\n Do you want to calculate another value (y/n): “;

cin >> quit;

if (quit == ‘y’ || quit == ‘Y’)

goto startagain;

break;

case ‘c’:

case ‘C’:

cout << ” Please enter temprature in Celcius: “;

cin >> c_temp;

k_temp = c_temp + 273.15 ;

f_temp = c_temp  *  9/5 + 32;

cout << ” Kelvin =” << k_temp;

cout << “\n Farhenheit =” << f_temp;

cout << “\n Do you want to calculate another value (y/n): “;

cin >> quit;

if (quit == ‘y’ || quit == ‘Y’)

goto startagain;

break;

case ‘k’:

case ‘K’:

cout << ” Please enter temprature in Kelvin: “;

cin >> k_temp;

c_temp = k_temp – 273.15 ;

f_temp = (k_temp – 273.14)  * 9/5 + 32;

cout << ” Celcius =” << c_temp;

cout << “\n Farhenheit =” << f_temp;

cout << “\n Do you want to calcute another value (y/n): “;

cin >> quit;

if (quit == ‘y’ || quit == ‘Y’)

goto startagain;

break;

default:

cout << “\n Invalid Choice”;

}

cout << endl<<endl;

system(“pause”);

}

[/box]