0% found this document useful (0 votes)
4 views

Lab 2

The document contains a series of C++ programming exercises that cover various programming concepts such as variable declaration, user input, arithmetic operations, conditional statements, and loops. Each exercise includes a code snippet that demonstrates the implementation of the specified task, such as calculating costs, exchanging values, and converting temperatures. The exercises are designed to help learners practice and understand fundamental programming skills in C++.

Uploaded by

thedeptraipro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 2

The document contains a series of C++ programming exercises that cover various programming concepts such as variable declaration, user input, arithmetic operations, conditional statements, and loops. Each exercise includes a code snippet that demonstrates the implementation of the specified task, such as calculating costs, exchanging values, and converting temperatures. The exercises are designed to help learners practice and understand fundamental programming skills in C++.

Uploaded by

thedeptraipro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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()
{
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;
}

Exercise 7: 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, 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;}

else if (b==0 && a!=0) {


cout<<"a cannot divide b."<<endl;
cout<<b<<" / "<<a<<" = "<<b/a;
return 0;}
else if (a==0 && b!=0) {
cout<<"b cannot divide a."<<endl;
cout<<a<<" / "<<b<<" = "<<a/b<<endl;
return 0;};
cout<<a<<" / "<<b<<" = "<<a/b<<endl;
cout<<b<<" / "<<a<<" = "<<b/a;
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;
}

You might also like