Cấu trúc :
While(condititon)
{
statement;
}.
-Chức năng : dùng để lặp lại các cấu trúc lệnh cho đến khi hoàn thành điều kiện
condition : điều kiện của vòng lặp
VD :
statment :là một lệnh hoặc chuỗi lệnh, nếu là 1 chuỗi lênh thì sẽ được đặt trong {}
II-Bài Tập
Bài1. Declare a variable which has the age of the person. Print the user’s name as many times as his age
Bài2. The program displays even numbers from 1 to 30.
Bài3. The program displays numbers from 10 to 0 in the reverse order
Bài4. The program will accept integers and display them until zero (0) is entered
Bài5. Find the factorial of a number.
Hint:
- Get the number.
- To begin, set the factorial of the number to be one.
- While the number is greater than one
- Set the factorial to be the factorial multiplied by the number.
- Decrement the number.
- Print out the factorial
Bài6. Write a program to print the series 100, 95 , 90, 85,………., 5.
Bài7. Accept two numbers num1 and num2. Find the sum of all odd numbers between the two
numbers entered .
Bài 8. Write a program to generate the Fibonacci series. (1,1,2,3,5,8,13,………)
Bài Làm Bài 1 :
#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 a=1;
while(a<=19){
printf("\t%dNguyen Huy",a);
a=a+1;
}
return 0;
}
Bài 2:
#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 a=1;
while(a<=30){
printf("\t%d",a);
a=a+1;
}
return 0;
}
Bài 3 :
#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 a=10;
while(a>=1){
printf("\t%d",a);
a=a-1;
}
return 0;
}
Bài 4 : .
#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 a;
while(a!=0){
printf("Nhap Vao 1 So :");
scanf("%d",&a);
}
printf("Ket Thuc.");
return 0;
Bài 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 a,b,c;
printf("Nhao so: ");
scanf("%d",&a);
b=1;
c=1;
while (b<=a){
c = c*b;
b=b+1;
}
printf("%d! = %d",a,c);
return 0;
}
Bài 6:
#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=100;
while(x>=1){
printf("\t%d",x);
x=x-5;
}
return 0;
}
Bài 7
#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 a,b,c;
printf("Nhap A= ");
scanf("%d",&a);
printf("Nhap B=");
scanf("%d",&b);
if (a%2==0) a=a+1;
while (a<=b){
c=b+a;
a=a+2;
}
printf("\nTONG SO LE TU A-B= %d",c);
return 0;
}
Bài 8 :
#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 a,b,c,d;
printf("Nhap So:");
scanf("%d",&a);
b=0;
c=1;
d=1;
for (b=0;b<=a;){
printf("%d ",d);
d=b+c;
c=b;
b=d;
}
return 0;
}
Kết luận : Qua bài này ta tìm hiểu được về cấu trúc và cách sử dụng của while, có thể rất đơn giản nhưng không thứ gì là quá dễ.









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