1 Vòng lặp for :
-Là lệnh trong chương trình dùng để lặp đi lặp lại các đoạn mã cho đến khi thỏa mãn điều kiện.
Cú pháp :
for(initialize couter; conditional test; re valuation)
{
satement;
}
trong thực hành initialize couter; conditional test; re valuation có thể có hoặc không nhưng bắt buộc trong () phải có 2 dấu ;; (;;) để chương trình có thể chạy.
-Ngoài vòng lặp for đơn chúng ta còn có vòng lặp for lồng nhau....
II.Bài Tập
Bài 1Declare a variable which has the age of the person. Print the user’s name as many times as his age :
Bài Làm
#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;
for(a;a<=19;a=a+1){
printf("\t%d Nguyen Huy",a);
}
return 0;
}
Bài 2 : The program displays even numbers from 1 to 30.
#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;
for(a;a<=30;a=a+1){
printf("\t%d ",a);
}
return 0;
}
Bài3: 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 a=10;
for(a;a>=1;a=a-1){
printf("\t%d ",a);
}
return 0;
}
Bài 4 : . The program will accept integers and display them until zero (0) is entered
#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;
for(;a!=0;){
printf("Nhap vao 1 so=");
scanf("%d",&a);
}
printf("Ket Thuc.");
return 0;
}
Bài 5 : 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#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,A;
printf("Nhap a=");
scanf("%d",&a);
b=1;
A=1;
for(b;b<=a;b=b+1){
A=A*b;
}
printf("Vay %d!=%d",a,A,a*b);
return 0;
}
Bài 6 :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 a=100;
for(a;a>=5;a=a-5){
printf("%d ",a);
}
return 0;
}
Bài 7 :
Accept two numbers num1 and num2. Find the sum of all odd numbers between the two
numbers entered .
#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,b;
printf("Nhap Num1 =");
scanf("%d",&num1);
printf("Nhap Num2 =");
scanf("%d",&num2);
if(num1%2==0)
num1=num1+1;
for(;num1<=num2;num1=num1+2){
b=num1+num2;}
printf("Tong cac so le :%d",b);
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,b,c,A;
a=0;
b=1;
c=1;
printf("Nhap A :");
scanf("%d",&A);
for(a=0;a<=A;){
c=a+b;
b=a;
a=c;
printf("\t%d",c);
}
return 0;
}
#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,A;
a=0;
b=1;
c=1;
printf("Nhap A :");
scanf("%d",&A);
for(a=0;a<=A;){
c=a+b;
b=a;
a=c;
printf("\t%d",c);
}
return 0;
}








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