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

Lab 2 InputOutput

The document provides 13 exercises for students to practice writing C++ programs that take inputs and perform calculations. The exercises include programs to print text, calculate areas and circumferences using formulas, exchange variable values, display student data, calculate roots of quadratic equations, and perform conversions between units of time. Students are instructed to write solutions for all exercises.

Uploaded by

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

Lab 2 InputOutput

The document provides 13 exercises for students to practice writing C++ programs that take inputs and perform calculations. The exercises include programs to print text, calculate areas and circumferences using formulas, exchange variable values, display student data, calculate roots of quadratic equations, and perform conversions between units of time. Students are instructed to write solutions for all exercises.

Uploaded by

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

HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM

Faculty of Computer Science and Engineering

Lab 2

In this week, we practice implementing in C++ small code with given input and output format.
Students can see some sample exercises and must prepare solution for all exercises in part B.

A. Sample exercises.
Exercise 1. Write a program to print “Hello World!” on the screen.

SOLUTION:
#include <iostream>
using namespace std;

int main(){
cout << “Hello World! ”;
cout << endl;
return 0;
}

Exercise 2. Write and run a program that performs the following steps:
- Reading a length a from the keyboard.
- Calculating the area of a square with length a by using the formula: S = a2.
- Displaying the area of square S.

SOLUTION:
#include <iostream>
using namespace std;

int main(){
int a; // Input – Length
int S; // Ouput – Area of Square

cout << "Enter the length a here: ";


cin >> a;

S = a*a;

cout << "The circumference: " << C << endl;


return 0;
}
B. Exercises must to do.

Introduction to Computing CO1005 – Semester 1 of 2019-2020 1/7


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

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 << " The id number:" << idnumber << "\n The number of units:" << units << "\n The price of 1
unit:" << price << "\n The cost of units:" << cost << endl;
tax = cost*0.06;
total = cost + tax;
cout <<" The tax is:"<< tax <<"\n The total cost is:"<< 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()
{
float A,B,C;
cout<<"Enter A: ";
cin >>A;
cout<<"Enter B: ";
cin >>B;
C=A;
A=B;
B=C;
cout<<"The new number A is: "<<A<<endl;
cout<<"The new number B 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.

Introduction to Computing CO1005 – Semester 1 of 2019-2020 2/7


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

#include <iostream>
using namespace std;
int main()
{
string name,age,sex,height,weight;
name="My name:Nguyễn Mạnh Phú Qúy";
age="Age:19";
sex="Sex:Male";
height="Height:160cm";
weight="Weight:65kg";
cout<<"My personal information: "<<endl;
cout<<name<<endl;
cout<<age<<endl;
cout<<sex<<endl;
cout<<height<<endl;
cout<<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()
{
float r,C;
cout<<"Enter radius: ";
cin>>r;
C=2*3.1415*r;
cout<<"The circumference 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()

Introduction to Computing CO1005 – Semester 1 of 2019-2020 3/7


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

{
float f,C;
cout<<"Enter Fahrenteit temperature: ";
cin>>f;
C = (f-32)*5/9;
cout<<"The Celcius temperature is: "<<C<<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 <math.h>
using namespace std;
int main()
{
float xA,xB,yA,yB,d;
cout<<"Enter coordinate of A: "<<endl;
cout<<"xA= ";
cin>>xA;
cout<<"yA= ";
cin>>yA;
cout<<"Enter coordinate of B: "<<endl;
cout<<"xB= ";
cin>>xB;
cout<<"yB= ";
cin>>yB;
d=sqrt((xB-xA)*(xB-xA)+(yB-yA)*(yB-yA));
cout<<"The distance is: "<<d<<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 <math.h>
using namespace std;
int main()
{

Introduction to Computing CO1005 – Semester 1 of 2019-2020 4/7


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

float a,b,c,delta,x1,x2;
cout<<"Enter a,b,c: "<<endl;
cout<<"a= ";
cin>>a;
cout<<"b= ";
cin>>b;
cout<<"c= ";
cin>>c;
delta=b*b-4*a*c;
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
cout<<"The real solutions are x1= "<<x1<<" x2= "<<x2<<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
displays the results.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a,b;
cout<<"Enter a,b: "<<endl;
cout<<"a= ";
cin>>a;
cout<<"b= ";
cin>>b;
cout<<a<<" + "<<b<<" = "<<a+b<<endl;
cout<<a<<" - "<<b<<" = "<<a-b<<endl;
cout<<a<<" x "<<b<<" = "<<a*b<<endl;
cout<<a<<" / "<<b<<" = "<<a/b<<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>
#include <math.h>
using namespace std;

Introduction to Computing CO1005 – Semester 1 of 2019-2020 5/7


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

int main()
{
int n;
cout<<"Enter n= ";
cin>>n;
(n % 2 ==0) ? cout<<n<<" is even" : cout<<n<<" is odd";
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>
#include <math.h>
using namespace std;
int main()
{
int a; float s;
cout<<"Enter a1= ";
cin>>a;
s=s+a;
cout<<"Enter a2= ";
cin>>a;
s=s+a;
cout<<"Enter a3= ";
cin>>a;
s=s+a;
cout<<"Enter a4= ";
cin>>a;
s=s+a;
cout<<"Enter a5= ";
cin>>a;
s=s+a;
s=s/5;
cout<<"The average is: "<<s<<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.

Introduction to Computing CO1005 – Semester 1 of 2019-2020 6/7


HCMC UNIVERSITY OF TECHNOLOGY, VNU-HCM
Faculty of Computer Science and Engineering

Student needs to assign value to number of days and display the result as example. Assume a year has
365 days.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int day,week,year;
cout<<"Enter the number of days= ";
cin>>day;
year=day/365;
week=(day-year*365)/7;
day=day-year*365-week*7;
cout<<year<<" years "<<week<<" weeks "<<day<<" days";
return 0;
}

Introduction to Computing CO1005 – Semester 1 of 2019-2020 7/7

You might also like