Thứ Ba, Tháng Hai 11, 2025
spot_img
HomeC++ exercises have solutions (sample code) about variables, data types and operators

C++ exercises have solutions (sample code) about variables, data types and operators

C++ program very popular in the programming world. If you are studying write C++ programsthen please refer to the exercises below.

C++ exercises on variables and data types

Write a program that requires the user to enter the width and height of a rectangle, then outputs the area and perimeter of that rectangle.

Sample code 1:

#include 
#include 
#include 
using namespace std;

int main()
{
  string swap;
Clrscr();

  cout > swap;
  while ( !(stringstream(swap) >> width) )
  {
    cout > swap;
  }
  
  cout > swap;
  while ( !(stringstream(swap) >> height) )
  {
    cout > swap;
  }

  cout 

Sample code 2:

//By Fate-X
#include

using std::cout;
using std::cin;

int main(){
    double width, height, area, perimeter;
    
    cout > width;
    cin.ignore();
    
    cout > height;
    cin.ignore();
    
    area = height * width;
    perimeter = 2 * (width + height);
    
    cout 

Write a program in C++ to find the size of the following basic data types:

sizeof(char) : 1 byte
sizeof(short) : 2 bytes
sizeof(int) : 4 bytes
sizeof(long) : 8 bytes
sizeof(long long): 8 bytes
sizeof(float): 4 bytes
sizeof(double): 8 bytes
sizeof(long double) : 16 bytes
sizeof(bool) : 1 byte

Sample code:

#include 
using namespace std;
 
int main() 
{
    cout 

Write a C++ program to print the welcome text on a separate line.

Sample code:

#include 
using namespace std;
 
int main()
{
    cout 

Write a C++ program to print the given lines of text

Exercise:

Write a C++ program to print the following lines:

I am 18 years old now.

I have many girls who want to follow me.

Sample code:

#include 
#include  
using namespace std; 
int main(int argc, char *argv[]) 
{ 
 int age; 
 age=10; 
 cout

Write a C++ program to print asterisks according to the given request.

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

Write a C++ program to declare two integer variables and one real variable, then print the results on the screen.

Request:

Write a C++ program to declare two integer variables, one real variable and assign the corresponding values ​​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

Write a C++ program that asks the user to enter their name, then prints 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>name; 
    cout

Write a C++ program to input three known integers, then print them to the screen in ascending and descending order.

Request:

Write a C++ program that requires the user to enter three integers, then prints those three 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 > num1 >> num2 >> num3;

    int output1 = num1;
    int output2 = num2;
    int output3 = num3;

    Sort(output1,output2,output3);

    std::cout 

Write a C++ program to print an integer, a real number, a predefined character.

Request:

Prints 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 

C++ exercises on operators

Write a program in C++ to print the sum of two numbers.

Sample code:

#include 
using namespace std;
 
int main()
{
    cout 

Find the sum, difference, product and quotient of two integers and print the results to the screen.

Request:

Assuming there are 2 given integers, calculate the sum, difference, product and quotient of the two numbers and then print the results to 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 

Enter two integers from the keyboard, calculate their sum, average and print it to the screen.

Request:

Enter 2 integers from the keyboard, calculate the sum and average of those two numbers, then print the results to the screen.

Sample code:

#include 
#include 
using namespace std; 
int main()
{

int x,y,tong;
float trungbinhcong;
cout >x>>y;
tong=x+y;
trungbinhcong=tong/2.0f;
cout 

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 result to the screen knowing the initial velocity, acceleration and time.

Request:

Suppose a car has an initial speed v0acceleration a and time t. Write a C++ program to find the final velocity of the car and print the results to the screen.

Suggest:

  • SUse the cin command to enter the corresponding values ​​for v0a and t.
  • Use the formula v = v0 + at to calculate final velocity

Sample code:

#include 
#include 
using namespace std; 
int main()
{

int v,u,a,t;
cout >u>>a>>t;
v=u+a*t;
cout 

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 the results as required.

Request:

Write a C++ program to print the results as follows:

Gtri x Gtri y Special Result
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

Suggest:

  • Use tab and newline 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

Result:

C++ exercise results

Write a C++ program to calculate exam scores.

Request:

Write a program to enter test scores, midterm exam scores, final exam scores, then calculate the total score and print the results to the screen as follows:

==========Inspection ================

Enter test point 1:10

Enter check point 2:9

Enter check point 3:10

==========Diem mid-term exam=============

Enter the mid-term test point: 8.5

===========Final Exam Table=============

Enter the final exam point: 10

Total inspection points: 29

Mid-term test score: 8.5

Final exam score: 10

…………………………….

Total points: 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>q1; 
    cout>q2; 
    cout>q3; 
    cout>diemgk; 
    cout>diemck; 
    cout

Result:

C++ exercise results

Enter 2 numbers x, y and calculate as required

Request:

Write a C++ program to enter two integers x, y, then calculate: p=x*y, s=x+y, q=s2+p(sx)*(p+y) and print the result to the screen.

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>x; 
    cout>y; 
    cout

Kết quả:

Nhap gia tri x: 5 

Nhap gia tri y: 10

Gia tri bieu thuc q la: 30225

C++ exercises apply binary operator overloading to add and subtract two complex numbers

Request:

Use the concept of operator overloading to add and subtract two complex numbers.

Suggest:

To use operator overloading in solving this complex number addition and subtraction exercise, you need to do the following:

  • Class declaration sophuc: Includes variables and member functions

  • Use the getvalue() function to get the real and imaginary parts of a complex number

  • Define the operator+() function to add two complex numbers

  • Define the operator-() function to subtract two complex numbers

  • Define display function

  • Declare complex numbers i1, i2 and calculation results kq1, kq2

  • Call the getvalue function to get the real and imaginary parts for i1, i2

  • Call the operator+() and operator-() functions to add and subtract two complex numbers

  • Call the display function to display 2 complex numbers and 2 addition and subtraction results

  • Returns values

Sample code:

#include
#include
using namespace std;  
class sophuc
{
              int a,b;
    public:
              void getvalue()
              {
                 cout>a>>b;
              }
              sophuc operator+(sophuc i)
              {
                            sophuc q;
                            q.a=a+i.a;
                            q.b=b+i.b;
                            return(q);
              }
              sophuc operator-(sophuc i)
              {
                            sophuc q;
                            q.a=a-i.a;
                            q.b=b-i.b;
                            return(q);
              }
              void display()
              {
                            cout

Result:

Results of C++ exercises on operator overloading

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments