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

  1. C ++ exercises about variables and data types
    1. Write a C ++ program to print the given text.
    2. Write a C ++ program to print the asterisks as required.
    3. Write a C ++ program to declare two integer variables and a real variable, then print the results on the screen.
    4. Writing a C ++ program requires the user to enter their name, and then print the results on the screen.
    5. Write a C ++ program to enter the three known integers, then print to the screen in ascending and descending order.
    6. Write a C ++ program to print an integer, a real number, a predefined character.
  2. C ++ exercises on operators
    1. Find the sum, effect, volume and quotient of the two integers and print the results to the screen.
    2. Enter two integers from the keyboard, calculate their sum, average and print to the screen.
    3. Find the final velocity and print the results to the screen when knowing the initial velocity, acceleration and time.
    4. Write a C ++ program to calculate expression values ​​and print results as required.
    5. Write a C ++ program to calculate test scores.
    6. Enter 2 x and y numbers and calculate as required
    7. 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:

  1. Use the cin command to enter the corresponding values ​​for v 0 , a and t.
  2. 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 = 0

Suggestions:

  1. 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:

Picture 1 of C ++ exercises have solutions (sample code) for variables, data types, and operators

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:

========== Point to check ================
Click to find out 1:10
Click the 2: 9 check box
Click to find 3:10 check
========== Stealthy test score =============
Download the exam: 8.5
=========== Examination time =============
Download the exam: 10
Check out information: 29
Score test: 8.5
Exam results: 10
......
Tong diem: 47.5

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:

Picture 2 of C ++ exercises have solutions (sample code) for variables, data types, and operators

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:

  1. Declaring the sophuc class: Include variables and member functions

  2. Use the getvalue () function to get the real part, the virtual part of the complex number

  3. Define operator + () function to add two complex numbers

  4. Define operator- () function to subtract two complex numbers

  5. Definition of display function

  6. Declare complex numbers i1, i2 and calculation results kq1, kq2

  7. Call the getvalue function to get the real part and the virtual part for i1, i2

  8. Call the function operator + () and operator- () to add and subtract two complex numbers

  9. Calling the display function to display 2 complex numbers, 2 results plus and minus

  10. 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:

Picture 3 of C ++ exercises have solutions (sample code) for variables, data types, and operators

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile