Friday, 17 April 2015

C++ Program to Convert a Range of Temperature in Celsius scale to Fahrenheit scale and Kelvin scale using ‘for’ loop and ‘while’ loop

AIM
Write and execute a C++ program to convert a range of temperature in Celsius scale to Fahrenheit scale and Kelvin scale using ‘for’ loop and ‘while’ loop. Do the conversions from 100C to 300C with an interval of 20C.

ALGORITHM
Step 1.               Start
Step 2.               Declare variables  ctemp,ftemp and ktemp.
Step 3.               Write “Celcius”, “Fahrenheit” and “Kelvin” with proper spacing.
Step 4.               Repeat the steps with ctemp=10 till ctemp=30.
Step 5.               Fahrenheit,  ftemp=(ctemp*1.8)+32
      Kelvin, ktemp=(ctemp+273.15)
      ctemp=ctemp+2
Step 6.               Write ctemp, ftemp, ktemp.
Step 7.               Stop.




PROGRAM
1.       Using ‘for’ loop

#include<iostream>
#include<iomanip>    // for setw()
using namespace std;
int main()
{
float ctemp,ftemp,ktemp;
cout<<"\nCelcius"<<setw(20)<< "Fahrenheit"<<setw(20)<< "Kelvin \n";
for(ctemp=10;ctemp<=30;ctemp=ctemp+2)
{
ftemp=(ctemp*1.8)+32;
ktemp=(ctemp+273.15);
cout<<"\n\n"<<ctemp<<setw(20)<<ftemp<<setw(23)<<ktemp;
}
return(0);
}

2.       Using ‘while’ loop

#include<iostream>
#include<iomanip>    // for setw()
using namespace std;
int main()
{
float ctemp=10,ftemp,ktemp;
cout<<"\nCelcius"<<setw(20)<< "Fahrenheit"<<setw(20)<< "Kelvin \n";
while(ctemp<=30)
{
ftemp=(ctemp*1.8)+32;
ktemp=(ctemp+273.15);
cout<<"\n\n"<<ctemp<<setw(20)<<ftemp<<setw(23)<<ktemp;
ctemp=ctemp+2;
}
return(0);
}

OUTPUT

Celcius          Fahrenheit            Kelvin


10                  50                 283.15

12                53.6                 285.15

14                57.2                 287.15

16                60.8                 289.15

18                64.4                 291.15

20                  68                 293.15

22                71.6                 295.15

24                75.2                 297.15

26                78.8                 299.15

28                82.4                 301.15


30                  86                 303.15

No comments :

Post a comment

Related Posts Plugin for WordPress, Blogger...