Chapter 1 - Input Output
Chapter 1 - Input Output
Chapter 1: Input/Output
Problem 1: Write a program to print “Hello World” on the screen.
Problem 2: Modify the problem 1 to allow enter name. Display the greeting message on the
screen.
Input: “Name:” Alex
Output: “Welcome Alex to join Yuan Ze University”
Problem 3: Write a program to input 3 integers. Print the sum of 3 numbers to the screen.
Problem 4: Write a program to input day, month and year of current date. Display the date on
the screen with format dd/mm/yyyy.
Input: 12 8 1994
Output: 12/8/1994
Problem 5: Write a program to input your birthdate and current year. Print out your age on the
screen.
Input: 12 3 2001 2022
Output: Your age is 21
Problem 6*: Write a program to input your birthdate. Using system time to calculate your age
and display it on the screen
Suggestion: Using ctime library
#include <iostream>
#include <ctime>
using namespace std;
int main(){
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "Year:" << 1900 + ltm->tm_year << endl;
}
Problem 7: Write a program to input your birthdate and display it as dd/mm/yy format.
Input: 3 8 2004
Output: 03/08/04
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Problem 8: This course will be evaluated with 1 paper test and 4 exercises. Write a program
that allows input a student name and scores from 1 test and 4 exercises. The final score will be
calculated as the average of the scores. Display student and final score with 2 decimal places on
the screen.
Input: Alex Chen
8.5 4 7.5 10 9.5
Output: Hi Alex Chen, you got 7.9 as final score
Suggestion:
• Input a string:
#include <iostream>
using namespace std;
int main(){
string fullName;
cout << "Your name: ";
getline (cin, fullName);
cout << "Your name is " << fullName;
}
• Rounding a floating point number with 2 decimal places
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double a = 10.432;
cout << fixed << setprecision(2) << a << endl;
return 0;
}
Problem 9: Write a program that allows you to input the diameter of a circle. Print the area of
the circle to the screen.
Suggestion: 𝐴 = 𝜋𝑟 2 𝜋 ≈ 3.14
#include <iostream>
#include <cmath>
int main (){
cout << "7 ^ 3 = " << pow (7.0, 3.0) << endl;
cout << "4.73 ^ 12 = " << pow (4.73, 12.0) << endl;
cout << "32.01 ^ 1.54 = " << pow (32.01, 1.54) << endl;
return 0;
}
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Problem 10: Write a program to input the lengths of the three sides in a triangle. Find the
perimeter and area of that triangle.
Suggestion: 𝑃 =𝑎+𝑏+𝑐
𝑎+𝑏+𝑐
𝑆= (semi-perimeter)
2
#include <iostream>
#include <cmath>
int main ()
{
double param, result;
param = 1024.0;
result = sqrt(param);
cout << "sqrt(" << param << ") = " << result << endl;
return 0;
}
Problem 11: Write a program that allows you to enter a temperature value of Fahrenheit (°F).
Calculate the corresponding Celsius (°C) temperature value. Know the formula to convert
between these two values is:
5
𝑇 𝑜𝐶 = (𝑇 𝑜𝐹 − 32) ×
9
Problem 12: Write a program that allows you to enter a temperature value of Celsius (°C).
Calculate the corresponding Fahrenheit (°F) temperature value.
9
𝑇 𝑜𝐹 = 𝑇 𝑜𝐶 × + 32
5
Lecturer: Quang-Thai Ho