C ++ exercises have solutions (sample code) for variables, data types, and operators
In the previous C ++ exercises, TipsMake.com introduced, you were acquainted with many types of lessons, from simple to complex. In this C ++ exercise, we will become familiar with C ++ exercises on variables and data types in C ++.
In the previous C ++ exercises, TipsMake.com introduced, you were acquainted with many types of lessons, from simple to complex. In this C ++ exercise, we will become familiar with C ++ exercises on variables and data types in C ++.
C ++ exercises by category
- C ++ exercises about variables and data types
- Write a C ++ program to print the given text.
- Write a C ++ program to print the asterisks as required.
- Write a C ++ program to declare two integer variables and a real variable, then print the results on the screen.
- Writing a C ++ program requires the user to enter their name, and then print the results on the screen.
- Write a C ++ program to enter the three known integers, then print to the screen in ascending and descending order.
- Write a C ++ program to print an integer, a real number, a predefined character.
- C ++ exercises on operators
- Find the sum, effect, volume and quotient of the two integers and print the results to the screen.
- Enter two integers from the keyboard, calculate their sum, average and print to the screen.
- Find the final velocity and print the results to the screen when knowing the initial velocity, acceleration and time.
- Write a C ++ program to calculate expression values and print results as required.
- Write a C ++ program to calculate test scores.
- Enter 2 x and y numbers and calculate as required
- C ++ exercises use the overloading of binary operators to add and subtract two complex numbers
C ++ exercises about variables and data types
Write a C ++ program to print the given text.
Exercise:
Write a C ++ program to print the following lines:
I'm 18 years old now.
I have a lot of people to follow.
Sample code:
#include #include using namespace std; int main(int argc, char *argv[]) { int age; age=10; cout<<" Toi nam nay "<
Write a C ++ program to print the asterisks as required.
Request:
Write a C ++ program to print asterisks in the following form:
*
**
***
****
*****
******
Sample code:
#include #include using namespace std; int main(int argc, char *argv[]) { cout<<"*n"; cout<<"**n"; cout<<"***n"; cout<<"****n"; cout<<"*****n"; cout<<"******n"; return 0; }
Write a C ++ program to declare two integer variables and a real variable, then print the results on the screen.
Request:
Write a C ++ program to declare two integer variables, a real variable and assign values of 8, 16 and 15.8 to them. Then print the results on the screen.
Sample code:
#include #include using namespace std; int main(int argc, char *argv[]) { int a; int b; float c; a=8; b=16; c=15.8; cout<<"a="<
Writing a C ++ program requires the user to enter their name, and then print the results on the screen.
Request:
Write a C ++ program to prompt the user to enter their name, then print the results on the screen.
Sample code:
#include #include using namespace std; int main(int argc, char *argv[]) { char name[20]; cout<<"Hay nhap ten cua ban:"; cin>>name; cout<<"Xin chao "<
Write a C ++ program to enter the three known integers, then print to the screen in ascending and descending order.
Request:
Writing a C ++ program requires the user to enter three integers, then print those 3 numbers to the screen in ascending order.
Sample code:
#include void Sort(int &a, int &b, int &c){ if(a>b){ int tmp = a; a = b; b = tmp; } if(a>c){ int tmp = a; a=c; c = tmp; } if(b>c){ int tmp = b; b=c; c=tmp; } return; } int main(){ std::cout << "Nhap 3 so nguyen: " << std::endl; int num1; int num2; int num3; std::cin >> num1 >> num2 >> num3; int output1 = num1; int output2 = num2; int output3 = num3; Sort(output1,output2,output3); std::cout << num1 << " " << num2 << " " << num3 << " " << " khi sap xep tang dan: "; std::cout << output1 << " " << output2 << " " << output3 << std::endl; return 0; }
Write a C ++ program to print an integer, a real number, a predefined character.
Request:
Print the following values on the standard screen: An integer a, a real number b and a character c.
Sample code:
#include #include using namespace std; int main() { int a = 100; float b = 5.1; char c = 'q'; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; return 0; }
C ++ exercises on operators
Find the sum, effect, volume and quotient of the two integers and print the results to the screen.
Request:
Suppose there are two given integers, calculate the sum, effect, volume and quotient of the two numbers and print the results on the screen.
Sample code:
#include #include using namespace std; int main() { int x = 12; int y = 5; int tong, hieu, tich, thuong; tong = x + y; hieu = x - y; tich = x * y; thuong = x / y; cout << "Tong cua " << x << " va " << y << " la " << tong << "." << endl; cout << "Hieu cua " << x << " va " << y << " la " << hieu << "." << endl; cout << "Tich cua " << x << " va " << y << " la " << tich << "." << endl; cout << "Thuong cua " << x << " va " << y << " la " << thuong << "." << endl; return 0; }
Enter two integers from the keyboard, calculate their sum, average and print to the screen.
Request:
Enter two integers from the keyboard, calculate the sum, the average of the two numbers, and then print the results to the screen.
Sample code:
#include #include using namespace std; int main() { int x,y,tong; float trungbinhcong; cout << "Nhap hai so nguyen : " << endl; cin>>x>>y; tong=x+y; trungbinhcong=tong/2.0f; cout << "Tong cua " << x << " va " << y << " la " << tong << "." << endl; cout << "Trung binh cong cua " << x << " va " << y << " la " << trungbinhcong << "." << endl; return 0; }
Result:
Nhap hai so nguyen:
3
5
Tong cua 3 va 5 la 8.
Trung binh cong cua 3 va 5 la 4.
Find the final velocity and print the results to the screen when knowing the initial velocity, acceleration and time.
Request:
Suppose an automobile has initial velocity v 0 , acceleration a and time t. Write a C ++ program to find the final speed of the car and print the results to the screen.
Suggestions:
- Use the cin command to enter the corresponding values for v 0 , a and t.
- Use the formula v = v 0 + at to calculate the final velocity
Sample code:
#include #include using namespace std; int main() { int v,u,a,t; cout << "Nhap van toc v0, gia toc a, thoi gian t: " << endl; cin>>u>>a>>t; v=u+a*t; cout << "Van toc cuoi cung la " << v << "." << endl; return 0; }
Result:
Nhap van toc v0, gia toc a, thoi gian t:
10 2 15
Van toc cuoi cung la 40.
Write a C ++ program to calculate expression values and print results as required.
Request:
Write a C ++ program to print the results as follows:
Gtri x Gtri y Bieu Thieu Thuc Ket 18 | 2 | A = y + 3 | A = 5 18 | 2 | B = y-2 | B = 0 18 | 2 | C = y * 5 | C = 10 18 | 2 | D = x / y | D = 9 18 | 2 | E = x% y | E = 0Suggestions:
- Use tabs and newlines to print on demand.
Sample code:
#include #include using namespace std; int main(int argc, char *argv[]) { int x; int y; x=18; y=2; cout<<"Ket qua tra ve:n"; cout<<"Gtri xt"<<"Gtri yt"<<"Bieu thuct"<<"Ket quan"; cout<
Result:
Write a C ++ program to calculate test scores.
Request:
Write a program to enter midterm test scores, test scores, final exam scores, then calculate the total score and print the results to the screen as follows:
Sample code:
#include #include #include using namespace std; int main(int argc, char *argv[]) { float q1; float q2; float q3; float tongdiemkt; float diemgk; float diemck; float tongdiem; cout<<"==========Diem kiem tra================n"; cout<<"Nhap diem kiem tra 1:"; cin>>q1; cout<<"n"; cout<<"Nhap diem kiem tra 2:"; cin>>q2; cout<<"n"; cout<<"Nhap diem kiem tra 3:"; cin>>q3; cout<<"n"; cout<<"==========Diem thi giua ky=============n"; cout<<"Nhap diem thi giua ky:"; cin>>diemgk; cout<<"n"; cout<<"===========Diem thi cuoi ky=============n"; cout<<"Nhap diem thi cuoi ky:"; cin>>diemck; cout<<"n"; tongdiemkt=q1+q2+q3; tongdiem=tongdiemkt+diemgk+diemck; cout<<"Tong diem kiem tra:"<
Result:
Enter 2 x and y numbers and calculate as required
Request:
Write a C ++ program to enter two integers x, y, then calculate: p = x * y, s = x + y, q = s 2 + p (sx) * (p + y) and print the results to the screen form.
Sample code:
#include #include #include using namespace std; int main(int argc, char *argv[]) { float x; float y; float p; float s; float q; cout<<"Nhap gia tri x: "; cin>>x; cout<<"n"; cout<<"Nhap gia tri y: "; cin>>y; cout<<"n"; p=x*y; s=x+y; q=s*s+p*(sx)*(p+y); cout<<"Gia tri bieu thuc q la: "<
Result:
Nhap gia tri x: 5
Nhap gia tri y: 10
Gia tri bieu thuc q la: 30225
C ++ exercises use the overloading of binary operators to add and subtract two complex numbers
Request:
Use the concept of overloading operators to add and subtract two complex numbers.
Suggestions:
To use operator overloading in this addition and subtraction problem, you need to do the following:
-
Declaring the sophuc class: Include variables and member functions
-
Use the getvalue () function to get the real part, the virtual part of the complex number
-
Define operator + () function to add two complex numbers
-
Define operator- () function to subtract two complex numbers
-
Definition of display function
-
Declare complex numbers i1, i2 and calculation results kq1, kq2
-
Call the getvalue function to get the real part and the virtual part for i1, i2
-
Call the function operator + () and operator- () to add and subtract two complex numbers
-
Calling the display function to display 2 complex numbers, 2 results plus and minus
-
Returns values
Sample code:
#include #include using namespace std; class sophuc { int a,b; public: void getvalue() { cout<<"Hãy nhập phần thực, phần ảo:"; cin>>a>>b; } sophuc operator+(sophuc i) { sophuc q; qa=a+ia; qb=b+ib; return(q); } sophuc operator-(sophuc i) { sophuc q; qa=ai.a; qb=bi.b; return(q); } void display() { cout<
Result:
You should read it
- 12 exercises to have strong legs for all ages
- Please download VnDoc's Exercises app to make learning easier
- C ++ exercises about IF ELSE
- 8 back pain relief exercises you can do right at your desk
- C ++ exercises have solutions (sample code)
- Exercises Microsoft .NET Framework & VB.NET
- Energetic new day with 10 effective exercises
- Reduce the risk of office diseases thanks to 5 on-site exercises
- Synthesis of the best exercise solution on smartphones
- Summary of JavaScript exercises with sample code
- Some simple stress relief and eye relaxation exercises
- Effective shoulder pain treatment for office workers
Maybe you are interested
How to Record Screen Video on Windows 7 Video: Russia launches the first lunar probe in nearly 50 years 5 best laptops for 3D modeling What happens when the Moon is close to Earth, at a distance of 420 km like the International Space Station (ISS) today? Collapse OS, an open source operating system for the apocalypse, the darkest days of mankind Some small 'notes' about data backup, Windows installation and recovery