Lab 2
Lab 2
Add some
suitable declarations to complete the program and run it.
#include<iostream>
using namespace std;
int main()
{
float units, price, idnumber, cost, tax, total;
units = 5;
price = 12.5;
idnumber = 12583;
cost = price*units;
cout << idnumber << " " << units << " " << price << " " << cost <<
endl;
tax = cost*0.06;
total = cost + tax;
cout << tax << " " << total << endl;
return 0;
}
Exercise 4: Write a program that asks the user to type two integers A and B and
exchange the value of A and B. The program should display the new value of A and
B.
#include <iostream>
using namespace std;
int main()
{
int a, b;
int temp;
cout<<"The number a is: ";
cin>>a;
cout<<"The number b is: ";
cin>>b;
temp=a;
a=b;
b=temp;
cout<<"The new number a after exchange is:"<<a<<endl;
cout<<"The new number b after exchange is:"<<b;
return 0;
}
Exercise 5: Write and run a program that reads the name, age, sex, height and
weight of a student and displays with proper heading for each variable.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age;
double height, weight;
string name, sex;
cout<<"What is your name?\n";
getline(cin,name);
cout<<"What is your age?\n";
cin>>age;
cout<<"What is your sex? (Male or Female)\n";
cin>>sex;
cout<<"What is your height? (Ex: 180)\n";
cin>>height;
cout<<"What is your weight?\n";
cin>>weight;
cout<<"So:"<<endl;
cout<<"Your name is: "<<name<<endl;
cout<<"Your age is: "<<age<<endl;
cout<<"Your sex is: "<<sex<<endl;
cout<<"Your height is: "<<height<<endl;
cout<<"Your weight is: "<<weight;
return 0;
}
Exercise 6: Write and run a program that performs the following steps:
- Assigning value to the radius r.
- Calculating the circumference using the formula: C = 2πr.
- Displaying the circumference
#include <iostream>
using namespace std;
int main()
{
int r;
double C;
double PI = 3.14;
cout<<"The radius R of the circle is: ";
cin>>r;
C = 2*r*PI;
cout<<"The circumderence is:"<<C;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
double f, c;
cout<<"The value of the Fahrenheit temperature is: ";
cin>>f;
c = (5.0 / 9.0) * (f - 32);
cout<<f<<" Fahrenheit is equal to "<<c<<" Celsius";
return 0;
}
Exercise 8: Write and run a program that reads the coordinate of 2 points, A and B,
from the keyboard and then displays the distance between A and B.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x1, x2, y1, y2, d;
cout<<"The x of the A is: "; cin>>x1;
cout<<"The y of the A is: "; cin>>y1;
cout<<"The x of the B is: "; cin>>x2;
cout<<"The y of the B is: "; cin>>y2;
d = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
cout<<"The distance between A and B is: "<<d;
return 0;
}
Exercise 9: Given a function: F(X) = a*X2 + b*X + c Write and run a program that
performs the following steps:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x1, x2, a, b, c, del;
cout<<"The equation ax^2 + bx + c has:"<<endl;
cout<<"a = "; cin>>a;
cout<<"b = "; cin>>b;
cout<<"c = "; cin>>c;
if(a == 0)
{
cout<<"Choose another a!!";
}
if (a !=0 )
{
del = b*b - 4*a*c;
if (del<0)
{
cout<<"No solution for the equation.";
}
if (del == 0)
{
x1 = -b/a;
cout<<"The equation has x1 = x2 = "<<x1;
}
if (del > 0)
{
x1 = (-b + sqrt(del))/(2*a);
x2 = (-b - sqrt(del))/(2*a);
cout<<"The equation has x1 = "<<x1<<" and x2 = "<< x2;
}
}
return 0;
}
Exercise 10: Write and run a program that reads two integers through the keyboard
and performs simple arithmetic operations (i.e., addition, subtraction,
multiplication and division) and displays the results.
#include <iostream>
using namespace std;
int main()
{
float a, b;
cout<<"The value of number a is: ";
cin>>a;
cout<<"The value of number b is: ";
cin>>b;
cout<<a<<" + "<<b<<" = "<< a+b<<endl;
cout<<a<<" - "<<b<<" = "<< a-b<<endl;
cout<<b<<" - "<<a<<" = "<< b-a<<endl;
cout<<a<<" * "<<b<<" = "<< a*b<<endl;
if (a==0 && b==0) {cout<<"cannot divide";
return 0;}
Exercise 11. Write and run a program that reads an integer from the keyboard and
displays whether the number is odd or not? You MUST NOT use IF statement.
#include <iostream>
using namespace std;
int main() {
int number, result;
string output;
cout << "Enter an integer: ";
cin >> number;
result = number % 2;
output = (result == 0) ? "The number is even." : "The number is
odd.";
cout << output << endl;
return 0;
}
Exercise 12. Write a program that asks the user to type 5 integers and writes the
average of the 5 integers. This program uses only 2 variables.
#include <iostream>
using namespace std;
int main()
{
double a, i, sum, ave;
i=1;
sum = 0;
while (i<=5)
{
cout<<"Enter integer number "<< i<<": ";
cin>>a;
i = i+1;
sum = sum + a;
}
ave = sum/5;
cout<<"The average of 5 numbers below is: "<<ave;
return 0;
}
Exercise 13. Write a program that converts the number of days into years, weeks
and days.
#include <iostream>
using namespace std;
int main()
{
int years, weeks, days, day;
cout<<"The value of days: ";
cin>>days;
if (days<0) {cout<<"Error"; return 0;
}
years = days /365;
weeks = (days - years*365)/7;
day = days -years*365 - weeks*7;
cout<<days<<" days = "<<years<<" years + "<<weeks<<" weeks +
"<<day<<" days";
return 0;
}