0% found this document useful (0 votes)
27 views11 pages

LAB2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

LAB2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Exercise 3. Read the following C++ program for understanding.

Add some suitable


declarations to complete the program and run it.
#<include<iostream>
using namespace std;
int main(){
...
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;
#include<iostream>
using namespace std;

int main(){
int units = 5;
float price = 12.5;
int idnumber = 12583;
float cost = price*units;
cout << "ID: " << idnumber << "\n"
<< "Units: " << units << "\n"
<< "Price: " << price << "\n"
<< "Cost: " << cost << endl;
float tax = cost*0.06;
float total = cost + tax;
cout << "Tax: " << tax << "\n"
<< "Total: " << 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;
cout << "Type a and b: \n";
cin >> a >> b;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: \n"
<< "a = " << a << "\n"
<< "b = " << b << "\n";
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(){
string yourName, sex;
int age;
float height, weight;
cout << "Type your name: "; getline(cin, yourName);
cout << "Type your age: "; cin >> age;
cout << "Type your sex: "; cin >> sex;
cout << "Type your height: "; cin >> height;
cout << "Type your weight: "; cin >> weight;
cout << "Name: " << yourName << endl
<< "Age: " << age << endl
<< "Sex: " << sex << endl
<< "Height: " << height << endl
<< "Weight: " << weight << endl;
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() {
double r;
cout << "Type radius: "; cin >> r;
double C = 2 * 3.14 * r;
cout << "The circumference of the circle with radius " << r << " is:
" << C << endl;
return 0;
}
Exercise 7. Write and run a program that performs the following steps:
- Assigning value to a Fahrenheit temperature f.
- Calculating the equivalent Celsius temperature C using the formula: C = (5.0/9)(f – 32).
Displaying the Celsius temperature C.

#include <iostream>
using namespace std;

int main() {
double f;
cout << "Type temperature in Farenheit: "; cin >> f;
double C = (5.0 / 9) * (f - 32);
cout << "The equivalent Celsius temperature is: " << C << " degrees."
<< endl;
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 <cmath>
using namespace std;

int main() {
double x1, y1, x2, y2;
cout << "Enter the coordinates of point A (x1 y1): ";
cin >> x1 >> y1;
cout << "Enter the coordinates of point B (x2 y2): ";
cin >> x2 >> y2;
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
std::cout << "The distance between A and B is: " << distance << endl;
return 0;
}
Exercise 9. Given a function:
F(X) = a*X2 + b*X + c
Write and run a program that performs the following steps:
- Reading the value of a, b and c from the keyboard.
- Solving F(X) = 0 ( assume that the equation has two real distinct solutions X1 and X2).
- Displaying X1 and X2
.
#include <iostream>

#include <cmath>
using namespace std;

int main() {
double a, b, c;
cout << "Enter a, b, and c (ax^2 + bx + c = 0): \n";
cin >> a >> b >> c;
double del = b * b - 4 * a * c;

if (del > 0) {
double X1 = (-b + sqrt(del)) / (2 * a);
double X2 = (-b - sqrt(del)) / (2 * a);
cout << "X1 = " << X1 << " ; X2 = " << X2 << endl;
} else {
cout << "The equation does not have two distinct real solutions."
<< endl;
}
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 display the results.
#include <iostream>
using namespace std;

int main() {
int num1, num2;
cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: ";
cin >> num2;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
cout << "Sum: " << sum << endl;
cout << "Difference: " << difference << endl;
cout << "Product: " << product << endl;
if (num2 != 0) {
double quotient = static_cast<double>(num1) / num2;
cout << "Division: " << quotient << endl;
} else {
cout << "Cannot divide by zero." << endl;
}
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 num;
cout << "Enter an integer: ";
cin >> num;
cout << ((num % 2 != 0) && true ? "Odd" : "Even") << 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() {
int num, sum = 0;
cout << "Enter 5 integers: " << endl;
for (int i = 0; i < 5; i++) {
cin >> num;
sum += num;
}
cout << "The average is: " << (sum / 5.0) << endl;
return 0;
}
Exercise 13. Write a program that converts the number of days into years, weeks and
days.
Example: 1532 days = 4 years + 10 weeks + 2 days.
Student needs to assign value to number of days and display the result as example.
Assume a year has 365 days.

#include <iostream>
using namespace std;

int main() {
int totalDays;
cout << "Type days: ";
cin >> totalDays;
int years = totalDays / 365; // 1 year = 365 days
int remainingDays = totalDays % 365;
int weeks = remainingDays / 7; // 1 week = 7 days
int days = remainingDays % 7;
cout << totalDays << " days = " << years << " years + " << weeks << "
weeks + " << days << " days." << endl;
return 0;
}

You might also like