I.Khái niệm
-Vòng lặp là chuỗi code thực hiện các lệnh giống nhau với số lượng xác định, cho tới khi điều kiện không còn thỏa mãn, thì vòng lặp vẫn tiếp tục thực hiện lệnh.
-Trong môn C thì ta sẽ học 3 loại vòng lặp:
+vòng lặp for
+vòng lặp while
+vòng lặp do...while.
-Điều kiện dùng đề cho vòng lặp tiếp tục thực hiện thì thể hiện "Mối quan hệ" và "Phép logic" giữa các giá trị biến.
II.Vòng lặp For
- Cấu trúc vòng lặp For về cơ bản như sau:
For(Biến đếm ; Điều kiện ; Biểu thức đánh giá);
{
Câu lệnh;
}
III.Bài tập
**Bài 1**:Declare a variable which has the age of the person. Print the user’s name as many times as his age.
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int i; int y; char YourName[20]; printf("Your age = "); scanf("%d", &i); printf("Your name is "); fflush(stdin); gets(YourName); for(y = 1; y <= i ; y = y+1) { printf("\n%s", YourName); } return 0; }
**Bài 2**:The program displays even numbers from 1 to 30.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i = 1;
for(;i <= 30 ; i = i+1)
{
if(i % 2 ==0)
printf("\n%d", i);
}
return 0;
}
**Bài 3**:The program displays numbers from 10 to 0 in the reverse order
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i = 10;
for(; i>= 0 ; i = i-1)
{
if(i >= 0 && i<= 10)
printf("\n%d", i);
}
return 0;
}
**Bài 4**:The program will accept integers and display them until zero (0) is entered
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x;
for(x=10;x!=0;)
{
printf("nhap vao mot so nguyen:");
scanf("%d",&x);
}
return 0;
}
**Bài 5**: Find the factorial of a number
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x=10,y=1;
printf("nhap vao mot so:");
scanf("%d",&x);
for(x;x>1;x=x-1){
y=y*x;
}
printf("luy thua cua %d la:%d",x,y);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x=10,y=1;
printf("nhap vao mot so:");
scanf("%d",&x);
for(x;x>1;x=x-1){
y=y*x;
}
printf("luy thua cua %d la:%d",x,y);
return 0;
}
**Bài 6**: Write a program to print the series 100, 95 , 90, 85,………., 5.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i = 100;
for(; i>= 5 ; i = i-5)
{
if(i % 5 ==0)
printf("\n%d", i);
}
return 0;
}
**Bài 7**: Accept two numbers num1 and num2. Find the sum of all odd numbers between the two
numbers entered int num1,num2,sum,x,max,min;
printf("nhap so num1:"); scanf("%d",&num1);
printf("nhap so num2:"); scanf("%d",&num2);
if (num1 > num2){
max = num1;
min = num2;
}else{
max = num2;
min = num1;}
x=min + 1;
for(x;(min<x) && (x<max);x++){
if (x%2 !=0) {
sum=sum+x; }
}
printf("the sum of all odd numbers between the two numbers entered: %d",sum);
return 0;
**Bài 8**:Write a program to generate the Fibonacci series. (1,1,2,3,5,8,13,………)
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int x=1,sum1,sum2=1,sum3=1; for(x;x<10;x++) { sum1=sum2; sum2=sum3; sum3=sum1+sum2; printf("\n%d",sum1); } return 0; }







Không có nhận xét nào:
Đăng nhận xét