Thứ Ba, 3 tháng 2, 2015

Day 4 (do...while)

                                              Vòng lặp do...while
I.Khái niệm
-Vòng lặp do...while hơi khác biệt với 2 vòng lặp trước đã đề cập, sử dụng 2 hàm do và while, cơ chế hoạt động của vòng lặp này là kiểm tra điều kiện ở cuối vòng lặp, sau khi mà vòng lặp đã được thực thi.Điều này cũng có nghĩa vòng lặp dowhile sẽ thực hiện ít nhất 1 lệnh, kể cả lệnh đó là lệnh sai.
II.Cấu Trúc
 -Cấu trúc cơ bản của vòng lặp này :
                Do  {
                      Câu lệnh;
                       } While (Điều kiện)

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 x;
do {
printf("\nTu Tu Bang: 18 Tuoi");
x=x+1;
} while(x<18);
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 x=2;
do {
printf("\n%d",x);
x=x+2;
} while(x<=30);
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 x=10;
do {
printf("% d",x);
x=x-1;
} while(x>=0);
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=10; while(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 a;
int n=1;
int gt=1;
printf("nhap 1 so:");
scanf("%d",&a);
do
{
gt=gt*n;
n++;
}
while(n <= a);
printf("ket qua cua so giai thua %d: %d",a,gt);
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 x=100;
do {
printf("% d",x);
x=x-5;
} while(x>=5);
return 0;
}

**Bài 7**:  Accept two numbers num1 and num2. Find the sum of all odd numbers between the two
 numbers 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 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;
do {
if (x%2 !=0) {
sum=sum+x; }
x++;
} while((min<x) && (x<max));
printf("tong cua cac so le giua hai so nhap vao la : %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;
do {
sum1=sum2;
sum2=sum3;
sum3=sum1+sum2;
printf("\n%d",sum1);
x++;
}while(x<10);
return 0;
}

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

Đăng nhận xét