Chapter 2: Programming Exercise

2.1

Write a program to display the following output using a single cout statement

Maths = 90
Physics = 77
Chemistry = 69

Answer
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
 
      char *sub[]={"Maths","Physics","Chemestry"};
      int mark[]={90,77,69};
      for(int i=0;i<3;i++)
      {
         cout<<setw(10)<<sub[i]<<setw(3)<<"="<<setw(4)<<mark[i]<<endl;
      }
    return 0;
}

OUTPUT

Maths = 90
Physics = 77
Chemistry = 69

2.2

Write a program to read two numbers from the keyboard and display the larger value on the screen.

Answer
#include<iostream>
#include<iomanip>
using namespace std; 
int main()
{
    float a,b;
        cout<<" Enter two values  :"<<endl;
    cin>>a>>b;
    if(a>b)
        cout<<" larger value = "<<a<<endl;
    else
        cout<<" larger value = "<<b<<endl;
    return 0;
}

OUTPUT

Enter two values : 10 20
larger value = 20

2.3

Write a program to input an integer from the keyboard and display on the screen “WELL DONE” that many times.

Answer
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
  int n;
  char *str;
  str="WELL DONE";
  cout<<" Enter an integer value ";
  cin>>n;
  for(int i=0;i<n;i++)
  {
    cout<<str<<endl;
  }
 return 0;
}

OUTPUT

Enter an integer value 5
WELL DONE
WELL DONE
WELL DONE
WELL DONE
WELL DONE

2.4

Write a program to read the values a, b and c and display x, where
x = a / b –c.
Test the program for the following values:
(a) a = 250, b = 85, c = 25
(b) a = 300, b = 70, c = 70

Answer
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
     float a,b,c,x;
     cout<<" Enter the value of a,b, &c :"<<endl;
     cin>>a>>b>>c;
     if((b-c)!=0)
     {
            x=a/(b-c);
            cout<<" x=a/(b-c) = "<<x<<endl;
     }
     else
     {
         cout<<"  x= infinity "<<endl;
     }
     return 0;
}

 

OUTPUT

Enter the value of a,b, &c : 300 70 70
x= infinity

2.5

Write a C++ program that will ask for a temperature in Fahrenheit and display it in Celsius

Answer
#include<iostream>
#include<iomanip>
using namespace std; 
int main()
{
    float f,theta;
    cout<<" Enter  the temperature in Feranhite    scale  : ";
    cin>>f;
    theta=((f-32)/9)*5;
    cout<<" Temperature in Celsius =  "<<theta<<endl;
    return 0;
}

 

OUTPUT

Enter the temperature in Feranhite scale : 105
Temperature in Celsius = 40.555557

2.6

Redo Exercise 2.5 using a class called temp and member functions.

Answer
#include<iostream>
#include<iomanip>
using namespace std; 
class temp
{
    float f,theta;
public:
    float conversion(float f);
};
 
float temp::conversion(float f)
{
    theta=((f-32)/9)*5;
    return theta;
}
int main()
{
    temp t;
    float f;
    cout<<" Enter temperature in Farenheite scale :"<<endl;
    cin>>f;
    cout<<" Temperature in Celsius scale = "<<t.conversion(f)<<endl;
    return 0;
}

 

OUTPUT

Enter the temperature in Feranhite scale : 112
Temperature in Celsius = 44.444443


Next Previous