<< Chapter < Page Chapter >> Page >

#include<stdio.h>

#include<conio.h>

int main ()

{ unsigned int n,i,tong;

clrscr();

printf("\n Nhap vao so nguyen duong n:");

scanf("%d",&n);

tong=0;

i=1;

do

{

tong+=i;

i++;

} while (i<=n);

printf("\n Tong tu 1 den %d =%d ",n,tong);

getch();

return 0;

}

Nếu chúng ta nhập vào số 9 thì kết quả như sau:

Ví dụ 3: Viết chương trình in ra trên màn hình một ma trận có n dòng m cột như sau (n, m>=1):

1 2 3 4 5 67

2345678

3456789

#include<stdio.h>

#include<conio.h>

int main ()

{ unsigned int dong, cot, n, m;

clrscr();

printf("\n Nhap vao so dong va so cot :");

scanf("%d%d",&n,&m);

dong=0;

do

{

printf("\n");

cot=1;

do

{

printf("%d\t",dong+cot);

cot++;

} while (cot<=m);

dong++;

} while (dong<n);

getch();

return 0;

}

Kết quả khi nhập 3 dòng 6 cột như sau

So sánh các vòng lặp

Vòng lặp for, while:

- Kiểm tra điều kiện trước thực hiện công việc sau nên đoạn lệnh thực hiện công việc có thể không được thực hiện .

- Vòng lặp kết thúc khi nào điều kiện sai.

Vòng lặp do…while:

- Thực hiện công việc trước kiểm tra điều kiện sau nên đoạn lệnh thực hiện công việc được thực hiện ít nhất 1 lần.

- Vòng lặp kết thúc khi nào điều kiện sai.

Các câu lệnh đặc biệt

Lệnh break

Cú pháp: break

Dùng để thoát khỏi vòng lặp. Khi gặp câu lệnh này trong vòng lặp, chương trình sẽ thoát ra khỏi vòng lặp và chỉ đến câu lệnh liền sau nó. Nếu nhiều vòng lặp -->break sẽ thoát ra khỏi vòng lặp gần nhất. Ngoài ra, break còn được dùng trong cấu trúc lựa chọn switch.

Lệnh continue

Cú pháp: continue

- Khi gặp lệnh này trong các vòng lặp, chương trình sẽ bỏ qua phần còn lại trong vòng lặp và tiếp tục thực hiện lần lặp tiếp theo.

- Ðối với lệnh for, biểu thức 3 sẽ được tính trị và quay lại bước 2.

- Ðối với lệnh while, do while; biểu thức điều kiện sẽ được tính và xét xem có thể tiếp tục thực hiện<Công việc>nữa hay không? (dựa vào kết quả của biểu thức điều kiện).

Bài tập

Mục đích yêu cầu

Làm quen và nắm vững các lệnh có cấu trúc của C, biết cách chọn lựa trong trường hợp nào sẽ sử dụng cấu trúc nào. Thực hiện các các chương trình trong phần nội dung bằng cách kết hợp các lệnh lặp, các lệnh rẽ nhánh và các lệnh đơn.

Nội dung

1. Viết chương trình nhập 3 số từ bàn phím, tìm số lớn nhất trong 3 số đó, in kết quả lên màn hình.

2. Viết chương trình tính chu vi, diện tích của tam giác với yêu cầu sau khi nhập 3 số a, b, c phải kiểm tra lại xem a, b, c có tạo thành một tam giác không? Nếu có thì tính chu vi và diện tích. Nếu không thì in ra câu " Không tạo thành tam giác".

3. Viết chương trình giải phương trình bậc nhất ax+b=0 với a, b nhập từ bàn phím.

4. Viết chương trình giải phương trình bậc hai ax2+bx + c = 0 với a, b, c nhập từ bàn phím.

5. Viết chương trình nhập từ bàn phím 2 số a, b và một ký tự ch.

Nếu: ch là “+“ thì thực hiện phép tính a + b và in kết quả lên màn hình.

ch là “–“ thì thực hiện phép tính a - b và in kết quả lên màn hình.

ch là “*” thì thực hiện phép tính a * b và in kết quả lên màn hình.

ch là “/” thì thực hiện phép tính a / b và in kết quả lên màn hình.

6. Viết chương trình nhập vào 2 số là tháng và năm của một năm. Xét xem tháng đó có bao nhiêu ngày? Biết rằng:

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Co nuoi. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10760/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Co nuoi' conversation and receive update notifications?

Ask