Cấu Trúc :
do {
statement;
}while(condition);
vòng lặp có tác dụng giống với for và while nhưng điều khác biệt là vòng lặp này kiểm tra điều kiện tại cuối vòng lặp
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ài8. 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;
do
{printf("\t%d Nguyen Huy",a);
a=a+1;
}
while(a<=19);
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 i=1;
do{printf("\t%d",i);
i=i+1;
}
while(i<=30);
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 i=10;
do{printf("\t%d",i);
i=i-1;
}
while(i>=0);
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 i;
do{ printf("Nhap I=");
scanf("%d",&i);
}
while(i!=0);
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 i,k,l;
printf("Nhap I =");
scanf("%d",&i);
k=1;
l=1;
do{ l=l*k;
k=k+1;
}
while(k<=i);
printf("%d!=%d",i,l);
return 0;
}
Bài 6
#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=100;
do{printf("\t%d",a);
a=a-5;
}
while(a>=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("Vao A va B: ");
scanf("%d%d",&a,&b);
if (a%2==0) a=a+1;
do{ printf("\n Tong Cac So Le: %d",c);
}
while (a<=b);{
c=b+a;
a=a+2;
}
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);
c=0;
d=1;
b=0;
do{
printf("%d ",b);
b=c+d;
d=c;
c=b;
}
while (b<=a);
printf("\n\n\n");
system("pause");
return 0;
}
Kết luận : Qua bài ta học được cách lặp do while dù đầu ra giống for, while nhưng tùy vào hoàn cảnh sẽ được sử dụng cho mục đích tối ưu nhất







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