Thứ Ba, 3 tháng 2, 2015

For

Filled under:

- Cú pháp tổng quát của vòng lặp for như sau:

for(khởi tạo giá trị cho biến điều khiển; biểu thức điều kiện; biểu thức thay đổi của biến điều khiển){
     câu lệnh;
}

Trong đó:
- Khởi tạo giá trị cho biến điều khiển là một câu lệnh gán giá trị ban đầu cho biến điều khiển trước khi thực hiện vòng lặp. Lệnh này chỉ được thực hiện duy nhất một lần.
- Biểu thức điều kiện là một biểu thức quan hệ, xác định điều kiện thoát cho vòng lặp.
- Biểu thức thay đổi của biến điều khiển xác định biến điều khiển sẽ bị thay đổi như thế nào sau mỗi lần vòng lặp được lặp lại
- Ba phần trên được ngăn cách bằng dấu chấm phảy. Câu lệnh trong thân vòng lặp có thể là một lệnh hoặc nhiều lệnh.

Ví dụ:
int i;
for(i = 1; i <= 10; i++){
    printf("%d", i);
}

- Trong các vòng lặp for, biểu thức điều kiện luôn được kiểm tra ngay khi bắt đầu vòng lặp. Do đó các lệnh trong thân vòng lặp sẽ không được thực hiện nếu ngay từ ban đầu điều kiện đó là sai.

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, a;
char name[30];
printf("Nhap so tuoi: ");
scanf("%d", &a); fflush(stdin);
printf("Nhap ten: ");
gets(name);
for(i = 1; i <= a; i++){
printf("%s \t", name);
}
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;
for(;x<=30;x=x+2){ printf("%d \t",x); } 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;
for(; x>=0 ;x=x-1){ printf("% d",x); } 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 i; 

 printf("Nhap i: "); 
 scanf("%d", &i);

 for(;i >=0; i--){
       printf("%d \t", i);
 } 
 return 0;
 }
*******************************************
Bài 5: Find the factorial of a number.
 Hint:
  1. Get the number.
  2. To begin, set the factorial of the number to be one.
  3. While the number is greater than one
  4. Set the factorial to be the factorial multiplied by the number.
  5. Decrement the number.
  6. Print out the factorial
*******************************************

#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 n, i;
int gt = 1;
printf("Nhap n: ");
scanf("%d", &n);
for(i=1; i<=n; i++){
gt=gt*i;
}
printf("Giai thua cua n la: %d", 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;

for(;x>=0;x=x-5)
{
     printf("% d \t",x);
}
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 i, a, b;
int x = 0;
printf("Nhap so a: ");
scanf("%d", &a);
printf("Nhap so b: ");
scanf("%d", &b);
if(a<b){for(i=a ; i<= b; i++){
if(i % 2 == 1){
x = x + i;
}
}
printf("Tong: %d", x);
}
else{for(i=b ; i<= a; i++){
if(i % 2 == 1){
x = x + i;
}
}
printf("Tong: %d", x);
}
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 a=0,b=1,c=1,i ;

for(a,b,c,i;i<=20;a=b,b=c,c=a+b,i++){
printf("%d",c);
}
return 0;
}

*******************************************

0 nhận xét:

Đăng nhận xét