peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikatukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Bài tập vòng lặp while và do while trong C++
STARTING
CONTROL STATEMENT
FUNCTION
ARRAY & POINTER
OOP
STL
ITERATORS
OTHER FEATURES
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Bài tập vòng lặp while và do while trong C++

Trong bài này mình sẽ tổng hợp các bài tâp vòng lặp while trong C++, bên cạnh mỗi bài mình cũng sẽ hướng dẫn giải bài tập bằng vòng lặp do while trong C++.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Nếu một bài toán được giải bằng vòng lặp while thì bạn hoàn toàn có thể sử dụng vòng lặp do while để thay thế. Tuy nhiên, theo cá nhân mình thì bạn chỉ nên sử dụng vòng lặp do while khi bài toán yêu cầu phải lặp tối thiểu một lần.

Sau đây mình xin tổng hợp một số bài tập C++ đơn giản nhưng sẽ giúp bạn rèn luyện kỹ năng lập trình của mình rất nhiều.

1. Bài tập vòng lặp while C++ thứ nhất

Đầu tiên hãy giải một bài cực kì đơn giản như sau:

Bài viết này được đăng tại [free tuts .net]

Bài tập 1: Hãy sử dụng vòng lặp while và do while để in ra các số từ 1 đến 100.

While + do while
#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    
    // Vong lap while
    while (i <= 100){
        cout << i << endl;
        i++;	
    }
    
    // Vong lap do while
    i = 1;
    do {
        cout << i << endl;
        i++;
    }
    while (i <= 100);
}

Tăng độ khó lên một xíu nhé.

Bài tập 2: In ra tất cả số chẵn từ 1 đến 100.

Bài này có hai cách giải như sau:

  • Cách thứ nhất là lặp với bước nhảy là 2,
  • Cách thứ hai là lặp với bước nhảy 1 nhưng phải kiểm tra thêm điều kiện là số chẵn hay số lẻ.

Mình sẽ giải cách 1 vì nó tối ưu hơn.

While
#include <iostream>
using namespace std;

int main()
{
    int i = 2;
    
    // Vong lap while
    while (i <= 100){
        cout << i << endl;
        i += 2;	
    }
}
Do while
#include <iostream>
using namespace std;

int main()
{
    int i = 2;
    // Vong lap do while
    do {
        cout << i << endl;
        i += 2;
    }
    while (i <= 100);
}

2. Bài tập vòng lặp while C++ thứ hai

Bài này sẽ hấp dẫn hơn một chút xíu.

Đề bài: Hãy viết chương trình yêu cầu người dùng nhập vào một số nằm trong khoảng từ 1 đến 100. Nếu người dùng nhập số khác thì yêu cầu nhập lại.

Đây là bài tập sử dụng vòng lặp nhưng không biết trước tổng số lần lặp là bao nhiêu, vì vậy ta chỉ có thể giải nó bằng vòng lặp while hoặc do while trong C++.

While
#include <iostream>
using namespace std;

int main()
{
    int num;
    
    while (true){
        cout << "Vui long nhap trong khoang 1 den 100: ";
        cin >> num;
        
        if (num >= 1 && num <= 100){
            break;
        }
        else {
            cout << "Ban nhap sai roi, hay nhap lai ..." << endl;
        }
    }
    
    cout << "So ban vua nhap la " << num << endl;
}
Do while
#include <iostream>
using namespace std;

int main()
{
    int num;
    
    do {
        cout << "Vui long nhap trong khoang 1 den 100: ";
        cin >> num;
        
        if (num >= 1 && num <= 100){
        	break;
        }
        else {
        	cout << "Ban nhap sai roi, hay nhap lai ..." << endl;
        }
    } while (true);
    
    cout << "So ban vua nhap la " << num << endl;
}

Đây là bài tập chúng ta thường sử dụng để kiểm tra dữ liệu người dùng nhập vào, nếu nhập không đúng thì yêu cầu nhập lại.

3. Bài tập vòng lặp while C++ thứ ba

Sử dung vòng lặp while để lặp qua các phần tử trong mảng.

Đề bài: Cho một mảng có độ dài 10 phần tử các số nguyên. Hãy viết chương trình C++ bằng cách sử dụng vòng lặp while để yêu cầu người dùng nhập dữ liệu cho mảng đó, sau đó sử dụng do while để in các phần tử lên màn hình.

#include <iostream>
using namespace std;
  
int main () {
    int numbers[10];
    
    cout << "Nhap phan tu cho mang." << endl;
    
    // Nhap phan tu
    int i = 0;
    while (i < 10){
    	cout << "Nhap phan tu thu " << i << ": ";
    	cin >> numbers[i];
    	i++;
    }
    
    // In gia tri cac phan tu
    i = 0;
    do {
    	cout << numbers[i] << endl;
    	i++;
    } while (i < 10);
    
    return 1;
}

Đúng ra bài này không nên giải bằng vòng lặp do while, bởi đây là vòng lặp sẽ được lặp ít nhất một lần, nên trường hợp mảng không có phần tử nào thì sẽ sinh lỗi. Tuy nhiên, vì ta khai báo 10 phần tử nên vẫn có thể sử dụng được vòng lặp do while để giải bài tập.

4. Bài tập vòng lặp while C++ thứ tư

Như mình có nói trong bài lý thuyết, vòng lặp while sử dụng trong trường hợp không biết tổng số lần lặp là bao nhiêu. Để chứng minh điều đó thì mình sẽ đưa ra một bài tập thế này.

Đề bài: Hãy viết chương trình yêu cầu người dùng nhập vào một số bất kì và in số đó lên màn hình. Nếu người dùng nhập vào số 0 thì thoát chương trình, ngược lại thì nhập liên tục.

Như bạn thấy, đây là dạng bài mà ta không hề biết trước tổng số lần lặp là bao nhiêu.

#include <iostream>
using namespace std;
  
int main () {
    int num;
    while (true){
    	cout << "Vui long nhap vao so bat ki, nhap 0 de thoat: ";
    	cin >> num;
    	
    	if (num == 0){
	    break;
	}
        else {
            cout << "So ban vua nhap la " << num << endl;
    	}
    }
    
    return 1;
}

Bạn hãy chạy thử để test nhé.

Lời kết: Trên là tổng hợp những bài tập vòng lặp while trong C++ cơ bản nhất, giúp bạn tiếp cận với ngôn ngữ lập trình C++ dễ dàng. Vì mình còn rất nhiều dạng bài tập khác nên nếu bạn muốn làm thêm thì hãy xem trong chuyên mục bài tập c++ nhé.

Cùng chuyên mục:

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý ngày tháng (datetime.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý chuỗi (string.h) trong C/C++

Các hàm xử lý chuỗi (string.h) trong C/C++

Thread Pools và Parallel Algorithms trong C++

Thread Pools và Parallel Algorithms trong C++

Tạo và quản lý các Multithreading trong C++

Tạo và quản lý các Multithreading trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Try, Catch, và Throw của Exception Handling trong C++

Try, Catch, và Throw của Exception Handling trong C++

Cách sử dụng Lambda Expressions trong C++

Cách sử dụng Lambda Expressions trong C++

Sử dụng weak_ptr trong C++

Sử dụng weak_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng unique_ptr trong C++

Sử dụng unique_ptr trong C++

Tổng quan về Smart Pointers trong C++

Tổng quan về Smart Pointers trong C++

Sử dụng Iterators trong STL của C++

Sử dụng Iterators trong STL của C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng trong List trong C++

[Iterator] Sử dụng trong List trong C++

[STL] Sử dụng Vector trong C++

[STL] Sử dụng Vector trong C++

Tổng quan về Iterators trong C++

Tổng quan về Iterators trong C++

Top