<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Bài tập C++ có giải &#40;code mẫu&#41; về biến, kiểu dữ liệu và toán tử</title>
<meta name="description" content="Bài tập C++ có giải &#40;code mẫu&#41; về biến, kiểu dữ liệu và toán tử - Savefile - Tin Tức -...">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Bài tập C++ có giải &#40;code mẫu&#41; về biến, kiểu dữ liệu và toán tử">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;bai-tap-c-co-giai-code-mau-ve-bien-kieu-du-lieu-va-toan-tu-6729.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/bai-tap-c-co-giai-code-mau-ve-bien-kieu-du-lieu-va-toan-tu-6729.html">
<link rel="shortcut icon" href="https://nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/bai-tap-c-co-giai-code-mau-ve-bien-kieu-du-lieu-va-toan-tu-6729.html">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://nguoicodonvn2008.info/">https://nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Bài tập C++ có giải &#40;code mẫu&#41; về biến, kiểu dữ liệu và toán tử</h1>
		<ul class="list-inline">
			<li>Thứ ba - 14/02/2023 23:13</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Bài tập C++ có giải &#40;code mẫu&#41; về biến, kiểu dữ liệu và toán tử" src="https://st.quantrimang.com/photos/image/2017/12/11/bai-tap-c-plus-plus-so-phuc-1.png" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p>C++ là một trong số ngôn ngữ lập trình phổ biến nhất hiện nay. Để sử dụng nó thành thạo, bạn có thể ôn luyện kiến thức về C++ qua các bài tập dưới đây. Trong phần bài tập C++ này, chúng ta sẽ làm quen với các bài tập C++ về biến và kiểu dữ liệu trong C++.</p>

<h2>&nbsp;</h2>

<h2 id="mcetoc_1c127sdfr0">Bài tập C++ về biến và kiểu dữ liệu</h2>

<h3>Viết một chương trình trong C++ để tìm kích thước của các kiểu dữ liệu cơ bản sau:</h3>

<p>sizeof(char) : 1 byte<br />
sizeof(short) : 2 byte<br />
sizeof(int) : 4 byte<br />
sizeof(long) : 8 byte<br />
sizeof(long long): 8 byte<br />
sizeof(float): 4 byte<br />
sizeof(double): 8 byte<br />
sizeof(long double) : 16 byte<br />
sizeof(bool) : 1 byte</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre0">
#include &lt;iostream&gt;
using namespace std;
int main() 
{
    cout &lt;&lt; &quot;\n\n Find Size of fundamental data types :\n&quot;;
	cout &lt;&lt; &quot;------------------------------------------\n&quot;;
   cout &lt;&lt; &quot; The sizeof(char) is :          &quot; &lt;&lt; sizeof(char) &lt;&lt; &quot; bytes \n&quot; ;
   cout &lt;&lt; &quot; The sizeof(short) is :         &quot; &lt;&lt; sizeof(short) &lt;&lt; &quot; bytes \n&quot; ;
   cout &lt;&lt; &quot; The sizeof(int) is :           &quot; &lt;&lt; sizeof(int) &lt;&lt; &quot; bytes \n&quot; ;
   cout &lt;&lt; &quot; The sizeof(long) is :          &quot; &lt;&lt; sizeof(long) &lt;&lt; &quot; bytes \n&quot; ;
   cout &lt;&lt; &quot; The sizeof(long long) is :     &quot; &lt;&lt; sizeof(long long) &lt;&lt; &quot; bytes \n&quot;;
   cout &lt;&lt; &quot; The sizeof(float) is :         &quot; &lt;&lt; sizeof(float) &lt;&lt; &quot; bytes \n&quot; ;
   cout &lt;&lt; &quot; The sizeof(double) is :        &quot; &lt;&lt; sizeof(double) &lt;&lt; &quot; bytes \n&quot;;
   cout &lt;&lt; &quot; The sizeof(long double) is :   &quot; &lt;&lt; sizeof(long double) &lt;&lt; &quot; bytes \n&quot;;
   cout &lt;&lt; &quot; The sizeof(bool) is :          &quot; &lt;&lt; sizeof(bool) &lt;&lt; &quot; bytes \n\n&quot;;
   return 0;
}
</pre>

<h3>Viết một chương trình C++ để in nội dung chào mừng trong một dòng riêng.</h3>

<p><strong>Code mẫu:</strong></p>

<pre id="pre1">
#include &lt;iostream&gt;
using namespace std;
int main()
{
    cout &lt;&lt; &quot;\n\n Print a welcome text in a separate line :\n&quot;;
	cout &lt;&lt; &quot;----------------------------------------------\n&quot;;
    cout &lt;&lt; &quot; Welcome to \n&quot; ;
    cout &lt;&lt; &quot; w3resource.com &quot;&lt;&lt;endl ;
}
</pre>

<h3 id="mcetoc_1c11r0qm00">Viết chương trình C++ để in các dòng text đã cho trước</h3>

<p><strong>Bài tập:</strong></p>

<p>Viết chương trình C++ để in các dòng sau:</p>

<p><em>Toi nam nay 18 tuoi.</em></p>

<p><em>Toi co nhieu hoai bao muon theo duoi.</em></p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre2">
#include &lt;cstdlib&gt;
#include &lt;iostream&gt; 
using namespace std; 
int main(int argc, char *argv&#91;&#93;) 
{ 
 int age; 
 age=10; 
 cout&lt;&lt;&quot; Toi nam nay &quot;&lt;&lt;age&lt;&lt;&quot; tuoi.\n&quot;; 
 cout&lt;&lt;&quot; Toi co nhieu hoai bao muon theo duoi.\n&quot;; 
 return 0; 
}</pre>

<h3 id="mcetoc_1c11r0qm01">Viết chương trình C++ để in các dấu sao theo yêu cầu đã cho.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Viết chương trình C++ để in các dấu sao theo dạng sau:</p>

<p>*</p>

<p>**</p>

<p>***</p>

<p>****</p>

<p>*****</p>

<p>******</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre3">
#include &lt;cstdlib&gt; 
#include &lt;iostream&gt; 
using namespace std; 
int main(int argc, char *argv&#91;&#93;) 
{ 
    cout&lt;&lt;&quot;*\n&quot;; 
    cout&lt;&lt;&quot;**\n&quot;; 
    cout&lt;&lt;&quot;***\n&quot;; 
    cout&lt;&lt;&quot;****\n&quot;; 
    cout&lt;&lt;&quot;*****\n&quot;;
    cout&lt;&lt;&quot;******\n&quot;; 
    return 0; 
}</pre>

<h3 id="mcetoc_1c11r0qm02">Viết chương trình C++ để khai báo 2 biến nguyên và một biến thực, sau đó in kết quả trên màn hình.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Viết chương trình C++ để khai báo hai biến nguyên, một biến thực và gán các giá trị tương ứng 8, 16 và 15.8 cho chúng. Sau đó in kết quả trên màn hình.</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre4">
#include &lt;cstdlib&gt; 
#include &lt;iostream&gt; 
using namespace std; 
int main(int argc, char *argv&#91;&#93;) 
{ 
    int a; 
    int b; 
    float c; 
    a=8; 
    b=16; 
    c=15.8; 
    cout&lt;&lt;&quot;a=&quot;&lt;&lt;a&lt;&lt;&quot;\t&quot;&lt;&lt;&quot;b=&quot;&lt;&lt;b&lt;&lt;&quot;\t&quot;&lt;&lt;&quot;c=&quot;&lt;&lt;c; 
    cout&lt;&lt;&quot;\n&quot;; 
    return 0; 
} </pre>

<h3 id="mcetoc_1c11r0qm03">Viết chương trình C++ yêu cầu người dùng nhập tên của họ, rồi in kết quả trên màn hình.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Viết chương trình C++ để nhắc người dùng nhập tên của họ, sau đó in kết quả trên màn hình.</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre5">
#include &lt;cstdlib&gt; 
#include &lt;iostream&gt; 
using namespace std;   
int main(int argc, char *argv&#91;&#93;) 
{ 
    char name&#91;20&#93;; 
    cout&lt;&lt;&quot;Hay nhap ten cua ban:&quot;; 
    cin&gt;&gt;name; 
    cout&lt;&lt;&quot;Xin chao &quot;&lt;&lt;name&lt;&lt;&quot;! \n&quot;; 
    return 0; 
}  </pre>

<h3 id="mcetoc_1c11r0qm04">Viết chương trình C++ để nhập ba số nguyên đã biết, sau đó in ra màn hình theo thứ tự tăng dần và giảm dần.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Viết chương trình C++ yêu cầu người dùng nhập ba số nguyên, sau đó in 3 số đó ra màn hình theo thứ tự tăng dần.</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre6">
#include &lt;iostream&gt;

void Sort(int &amp;a, int &amp;b, int &amp;c){
    if(a&gt;b){
       int tmp = a;
       a = b;
       b = tmp;
    }
    if(a&gt;c){
       int tmp = a;
       a=c;
       c = tmp;
    }
    if(b&gt;c){
       int tmp = b;
       b=c;
       c=tmp;
    }
    return;
}

int main(){
    std::cout &lt;&lt; &quot;Nhap 3 so nguyen: &quot; &lt;&lt; std::endl;
    int num1;
    int num2;
    int num3;
    std::cin &gt;&gt; num1 &gt;&gt; num2 &gt;&gt; num3;

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

    Sort(output1,output2,output3);

    std::cout &lt;&lt; num1 &lt;&lt; &quot; &quot; &lt;&lt; num2 &lt;&lt; &quot; &quot; &lt;&lt; num3 &lt;&lt; &quot; &quot;
         &lt;&lt; &quot; khi sap xep tang dan: &quot;;
    std::cout &lt;&lt; output1 &lt;&lt; &quot; &quot; &lt;&lt; output2 &lt;&lt; &quot; &quot; &lt;&lt; output3 &lt;&lt; std::endl;

    return 0;
}</pre>

<h3 id="mcetoc_1c11r0qm05">Viết chương trình C++ để in một số nguyên, một số thực, một ký tự đã được định nghĩa trước.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>In các giá trị sau trên màn hình chuẩn: Một số nguyên a, một số thực b và một ký tự c.</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre7">
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std; 
int main()
{
  int a = 100;
  float b = 5.1;
  char c = &#039;q&#039;;
  cout &lt;&lt; &quot;a = &quot; &lt;&lt; a &lt;&lt; endl;
  cout &lt;&lt; &quot;b = &quot; &lt;&lt; b &lt;&lt; endl;
  cout &lt;&lt; &quot;c = &quot; &lt;&lt; c &lt;&lt; endl;
  return 0;
}</pre>

<h2 id="mcetoc_1c127sdgb1">Bài tập C++ về toán tử</h2>

<h3>Viết một chương trình trong C++ để in tổng của hai số.</h3>

<p><strong>Code mẫu:</strong></p>

<pre id="pre8">
#include &lt;iostream&gt;
using namespace std;
int main()
{
    cout &lt;&lt; &quot;\n\n In tổng của hai số :\n&quot;;
	cout &lt;&lt; &quot;-----------------------------------\n&quot;;
    cout &lt;&lt; &quot; Tổng của 29 và 30 là : &quot;&lt;&lt; 29+30 &lt;&lt;&quot;\n\n&quot; ;
}
</pre>

<h3 id="mcetoc_1c127sdgb2">Tìm tổng, hiệu, tích và thương của hai số nguyên và in kết quả ra màn hình.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Giả sử có 2 số nguyên cho trước, bạn hãy tính tổng, hiệu, tích và thương của hai số rồi in kết quả ra màn hình.</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre9">
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
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 &lt;&lt; &quot;Tong cua &quot; &lt;&lt; x &lt;&lt; &quot; va &quot; &lt;&lt; y &lt;&lt; &quot; la &quot; &lt;&lt; tong &lt;&lt; &quot;.&quot; &lt;&lt; endl;
  cout &lt;&lt; &quot;Hieu cua &quot; &lt;&lt; x &lt;&lt; &quot; va &quot; &lt;&lt; y &lt;&lt; &quot; la &quot; &lt;&lt; hieu &lt;&lt; &quot;.&quot; &lt;&lt; endl;
  cout &lt;&lt; &quot;Tich cua &quot; &lt;&lt; x &lt;&lt; &quot; va &quot; &lt;&lt; y &lt;&lt; &quot; la &quot; &lt;&lt; tich &lt;&lt; &quot;.&quot; &lt;&lt; endl;
  cout &lt;&lt; &quot;Thuong cua &quot; &lt;&lt; x &lt;&lt; &quot; va &quot; &lt;&lt; y &lt;&lt; &quot; la &quot; &lt;&lt; thuong &lt;&lt; &quot;.&quot; &lt;&lt; endl;
  return 0;
}</pre>

<h3 id="mcetoc_1c127t6cu3">Nhập hai số nguyên từ bàn phím, tính tổng, trung bình cộng của chúng và in ra màn hình.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Nhập 2 số nguyên từ bàn phím, tính tổng, trung bình cộng của hai số đó, rồi in kết quả ra màn hình.</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre10">
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std; 
int main()
{

int x,y,tong;
float trungbinhcong;
cout &lt;&lt; &quot;Nhap hai so nguyen : &quot; &lt;&lt; endl;
cin&gt;&gt;x&gt;&gt;y;
tong=x+y;
trungbinhcong=tong/2.0f;
cout &lt;&lt; &quot;Tong cua &quot; &lt;&lt; x &lt;&lt; &quot; va &quot; &lt;&lt; y &lt;&lt; &quot; la &quot; &lt;&lt; tong &lt;&lt; &quot;.&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Trung binh cong cua &quot; &lt;&lt; x &lt;&lt;  &quot; va &quot; &lt;&lt; y &lt;&lt; &quot; la &quot; &lt;&lt; trungbinhcong &lt;&lt; &quot;.&quot; &lt;&lt; endl;
return 0;
}</pre>

<p><strong>Kết quả:</strong></p>

<pre id="pre11">
Nhap hai so nguyen:35Tong cua 3 va 5 la 8.Trung binh cong cua 3 va 5 la 4.</pre>

<h3 id="mcetoc_1c1283hbs4">Tìm vận tốc cuối cùng và in kết quả ra màn hình khi biết vận tốc ban đầu, gia tốc và thời gian.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Giả sử một chiếc ô tô có vận tốc ban đầu v<sub>0</sub>, gia tốc a và thời gian t. Viết chương trình C++ để tìm vận tốc cuối cùng của ô tô và in kết quả ra màn hình.</p>

<p><strong>Gợi ý:</strong></p>

<ul>
	<li><u>S</u>ử dụng lệnh cin để nhập các giá trị tương ứng cho v<sub>0</sub>, a và t.</li>
	<li>Dùng công thức v = v<sub>0</sub>&nbsp;+ a.t để tính vận tốc cuối cùng</li>
</ul>

<p><strong>Code mẫu:</strong></p>

<pre id="pre12">
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std; 
int main()
{

int v,u,a,t;
cout &lt;&lt; &quot;Nhap van toc v0, gia toc a, thoi gian t: &quot; &lt;&lt; endl;
cin&gt;&gt;u&gt;&gt;a&gt;&gt;t;
v=u+a*t;
cout &lt;&lt; &quot;Van toc cuoi cung la &quot; &lt;&lt; v &lt;&lt; &quot;.&quot; &lt;&lt; endl;
return 0;
}</pre>

<p><strong>Kết quả:</strong></p>

<pre id="pre13">
Nhap van toc v0, gia toc a, thoi gian t:10 2 15Van toc cuoi cung la 40.</pre>

<h3 id="mcetoc_1c128nd8g5">Viết chương trình C++ để tính các giá trị biểu thức và in kết quả theo yêu cầu.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Viết chương trình C++ để in kết quả dưới dạng sau:</p>

<table>
	<tbody>
		<tr>
			<td>Gtri x</td>
			<td>Gtri y</td>
			<td>Bieu Thuc</td>
			<td>Ket qua</td>
		</tr>
		<tr>
			<td>18 |</td>
			<td>2 |</td>
			<td>A=y+3</td>
			<td>|A=5</td>
		</tr>
		<tr>
			<td>18 |</td>
			<td>2 |</td>
			<td>B=y-2</td>
			<td>|B=0</td>
		</tr>
		<tr>
			<td>18 |</td>
			<td>2 |</td>
			<td>C=y*5</td>
			<td>|C=10</td>
		</tr>
		<tr>
			<td>18 |</td>
			<td>2 |</td>
			<td>D=x/y</td>
			<td>|D=9</td>
		</tr>
		<tr>
			<td>18 |</td>
			<td>2 |</td>
			<td>E=x%y</td>
			<td>|E=0</td>
		</tr>
	</tbody>
</table>

<p><strong>Gợi ý:</strong></p>

<ul>
	<li>Dùng tab và newline để có thể in theo yêu cầu.</li>
</ul>

<p><strong>Code mẫu:</strong></p>

<pre id="pre14">
#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
using namespace std;
int main(int argc, char *argv&#91;&#93;)
{
int x;
int y;
x=18;
y=2;
cout&lt;&lt;&quot;Ket qua tra ve:\n&quot;;
cout&lt;&lt;&quot;Gtri x\t&quot;&lt;&lt;&quot;Gtri y\t&quot;&lt;&lt;&quot;Bieu thuc\t&quot;&lt;&lt;&quot;Ket qua\n&quot;;
cout&lt;&lt;x&lt;&lt;&quot; |\t&quot;&lt;&lt;y&lt;&lt;&quot; |\t&quot;&lt;&lt;&quot;A=y+3\t&quot;&lt;&lt;&quot;\t|&quot;&lt;&lt;&quot;A=&quot;&lt;&lt;y+3&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;x&lt;&lt;&quot; |\t&quot;&lt;&lt;y&lt;&lt;&quot; |\t&quot;&lt;&lt;&quot;B=y-2\t&quot;&lt;&lt;&quot;\t|&quot;&lt;&lt;&quot;B=&quot;&lt;&lt;y-2&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;x&lt;&lt;&quot; |\t&quot;&lt;&lt;y&lt;&lt;&quot; |\t&quot;&lt;&lt;&quot;C=y*5\t&quot;&lt;&lt;&quot;\t|&quot;&lt;&lt;&quot;C=&quot;&lt;&lt;y*5&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;x&lt;&lt;&quot; |\t&quot;&lt;&lt;y&lt;&lt;&quot; |\t&quot;&lt;&lt;&quot;D=x/y\t&quot;&lt;&lt;&quot;\t|&quot;&lt;&lt;&quot;D=&quot;&lt;&lt;(float)(x/y)&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;x&lt;&lt;&quot; |\t&quot;&lt;&lt;y&lt;&lt;&quot; |\t&quot;&lt;&lt;&quot;E=x%y\t&quot;&lt;&lt;&quot;\t|&quot;&lt;&lt;&quot;E=&quot;&lt;&lt;x%y&lt;&lt;&quot;\n&quot;;

return 0;
}</pre>

<p><strong>Kết quả:</strong></p>

<p><img alt="Kết quả bài tập C++" data-i="0" data-src="https://st.quantrimang.com/photos/image/2017/12/11/bai-tap-c-plus-plus.png" data-was-processed="true" height="149" src="https://st.quantrimang.com/photos/image/2017/12/11/bai-tap-c-plus-plus.png" width="356" /></p>

<h3 id="mcetoc_1c12a29vk0">Viết chương trình C++ để tính điểm thi.</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Viết chương trình để nhập điểm kiểm tra, điểm thi giữa kỳ, điểm thi cuối kỳ, sau đó tính tổng điểm và in kết quả ra màn hình dưới dạng sau:</p>
==========Diem kiem tra================<br />
Nhap diem kiem tra 1:10<br />
Nhap diem kiem tra 2:9<br />
Nhap diem kiem tra 3:10<br />
==========Diem thi giua ky=============<br />
Nhap diem thi giua ky:8.5<br />
===========Diem thi cuoi ky=============<br />
Nhap diem thi cuoi ky:10<br />
Tong diem kiem tra: 29<br />
Diem thi giua ky: 8.5<br />
Diem thi cuoi ky: 10<br />
.................................<br />
Tong diem: 47.5
<p><strong>Code mẫu:</strong></p>

<pre id="pre15">
#include &lt;cstdlib&gt; 
#include &lt;iostream&gt; 
#include&lt;iomanip&gt; 
using namespace std; 
int main(int argc, char *argv&#91;&#93;) 
{ 
    float q1; 
    float q2; 
    float q3; 
    float tongdiemkt; 
    float diemgk; 
    float diemck; 
    float tongdiem; 
    cout&lt;&lt;&quot;==========Diem kiem tra================\n&quot;; 
    cout&lt;&lt;&quot;Nhap diem kiem tra 1:&quot;; 
    cin&gt;&gt;q1; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;Nhap diem kiem tra 2:&quot;; 
    cin&gt;&gt;q2; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;Nhap diem kiem tra 3:&quot;; 
    cin&gt;&gt;q3; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;==========Diem thi giua ky=============\n&quot;; 
    cout&lt;&lt;&quot;Nhap diem thi giua ky:&quot;; 
    cin&gt;&gt;diemgk; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;===========Diem thi cuoi ky=============\n&quot;; 
    cout&lt;&lt;&quot;Nhap diem thi cuoi ky:&quot;; 
    cin&gt;&gt;diemck; 
    cout&lt;&lt;&quot;\n&quot;; 
    tongdiemkt=q1+q2+q3; 
    tongdiem=tongdiemkt+diemgk+diemck; 
    cout&lt;&lt;&quot;Tong diem kiem tra:&quot;&lt;&lt;right&lt;&lt;setw(5)&lt;&lt;tongdiemkt; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;Diem thi giua ky:&quot;&lt;&lt;right&lt;&lt;setw(7)&lt;&lt;diemgk; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;Diem thi cuoi ky:&quot;&lt;&lt;right&lt;&lt;setw(10)&lt;&lt;diemck; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;.................................\n&quot;; 
    cout&lt;&lt;&quot;Tong diem:&quot;&lt;&lt;right&lt;&lt;setw(11)&lt;&lt;tongdiem; 
    cout&lt;&lt;&quot;\n&quot;; 
    return 0; 
}</pre>

<p><strong>Kết quả:</strong></p>

<p><img alt="Kết quả bài tập C++" data-i="1" data-src="https://st.quantrimang.com/photos/image/2017/12/11/bai-tap-c-plus-plus-1.png" data-was-processed="true" height="376" src="https://st.quantrimang.com/photos/image/2017/12/11/bai-tap-c-plus-plus-1.png" width="376" /></p>

<h3 id="mcetoc_1c12d00440">Nhập 2 số x, y và tính theo yêu cầu</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Viết chương trình C++ để nhập hai số nguyên x, y, sau đó tính: p=x*y, s=x+y, q=s<sup>2</sup>+p(s-x)*(p+y) và in kết quả ra màn hình.</p>

<p><strong>Code mẫu:</strong></p>

<pre id="pre16">

&nbsp;</pre>

<pre id="pre17">
#include &lt;cstdlib&gt; 
#include &lt;iostream&gt; 
#include&lt;iomanip&gt; 
using namespace std; 
int main(int argc, char *argv&#91;&#93;) 
{ 
    float x; 
    float y; 
    float p; 
    float s; 
    float q; 
    cout&lt;&lt;&quot;Nhap gia tri x: &quot;; 
    cin&gt;&gt;x; 
    cout&lt;&lt;&quot;\n&quot;; 
    cout&lt;&lt;&quot;Nhap gia tri y: &quot;; 
    cin&gt;&gt;y; 
    cout&lt;&lt;&quot;\n&quot;; 
    p=x*y; 
    s=x+y; 
    q=s*s+p*(s-x)*(p+y); 
    cout&lt;&lt;&quot;Gia tri bieu thuc q la: &quot;&lt;&lt;q; 
    cout&lt;&lt;&quot;\n&quot;; 
    return 0; 
} </pre>

<pre id="pre18">

&nbsp;</pre>

<p><strong>Kết quả:</strong></p>

<pre id="pre19">
Nhap gia tri x: 5 Nhap gia tri y: 10 Gia tri bieu thuc q la: 30225</pre>

<h3 id="mcetoc_1c12d00441">Bài tập C++ vận dụng nạp chồng toán tử nhị phân để cộng, trừ hai số phức</h3>

<p><strong>Yêu cầu:</strong></p>

<p>Sử dụng khái niệm nạp chồng toán tử để cộng và trừ hai số phức.</p>

<p><strong>Gợi ý:</strong></p>

<p>Để sử dụng nạp chồng toán tử trong giải bài tập cộng, trừ số phức này, bạn cần làm những việc sau:</p>

<ul>
	<li>
	<p>Khai báo lớp&nbsp;<strong>sophuc</strong>: Bao gồm các biến và hàm thành viên</p>
	</li>
	<li>
	<p>Dùng hàm getvalue() để nhận phần thực, phần ảo của số phức</p>
	</li>
	<li>
	<p>Định nghĩa hàm operator+() để cộng hai số phức</p>
	</li>
	<li>
	<p>Định nghĩa hàm operator-() để trừ hai số phức</p>
	</li>
	<li>
	<p>Định nghĩa hàm display</p>
	</li>
	<li>
	<p>Khai báo các số phức i1, i2 và kết quả phép tính kq1, kq2</p>
	</li>
	<li>
	<p>Gọi hàm getvalue để lấy phần thực và phần ảo cho i1, i2</p>
	</li>
	<li>
	<p>Gọi hàm operator+() và operator-() để cộng, trừ hai số phức</p>
	</li>
	<li>
	<p>Gọi hàm display để hiển thị 2 số phức, 2 kết quả cộng, trừ</p>
	</li>
	<li>
	<p>Trả về các giá trị</p>
	</li>
</ul>

<p><strong>Code mẫu:</strong></p>

<pre id="pre20">
#include&lt;iostream&gt;
#include&lt;conio.h&gt;
using namespace std;  
class sophuc
{
              int a,b;
    public:
              void getvalue()
              {
                 cout&lt;&lt;&quot;Hãy nhập phần thực, phần ảo:&quot;;
                 cin&gt;&gt;a&gt;&gt;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&lt;&lt;a&lt;&lt;&quot;+&quot;&lt;&lt;b&lt;&lt;&quot;i&quot;&lt;&lt;&quot;\n&quot;;
              }
};

int main()
{
   sophuc i1,i2,kq1,kq2;
   i1.getvalue();
   i2.getvalue();
   kq1 = i1+i2;
   kq2 = i1-i2;
   cout&lt;&lt;&quot;Số phức đã nhập vào:\n&quot;;
   i1.display();
   i2.display();
   cout&lt;&lt;&quot;Kết quả cộng, trừ số phức:\n&quot;;
   kq1.display();
   kq2.display();
   return 0;
}</pre>

<p><strong>Kết quả:</strong></p>

<p><img alt="Kết quả bài tập C++ về nạp chồng toán tử" data-i="2" data-src="https://st.quantrimang.com/photos/image/2017/12/11/bai-tap-c-plus-plus-so-phuc-1.png" data-was-processed="true" height="311" src="https://st.quantrimang.com/photos/image/2017/12/11/bai-tap-c-plus-plus-so-phuc-1.png" width="358" /></p>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/bai-tap-c-co-giai-code-mau-ve-bien-kieu-du-lieu-va-toan-tu-6729.html" title="Bài tập C++ có giải &#40;code mẫu&#41; về biến, kiểu dữ liệu và toán tử">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/bai-tap-c-co-giai-code-mau-ve-bien-kieu-du-lieu-va-toan-tu-6729.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="cron" src="/index.php?second=cronjobs&amp;p=v0d53Y5m" width="1" height="1" /></div>
<script src="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://nguoicodonvn2008.info",
            "logo": "https://nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>