Thứ Hai, 9 tháng 2, 2015

Day 6





1. Write a C program that accepts a number and square the number with the help of a function.
To do this,
a. Declare a function.
b. Accept the number.
c. Pass the number to the function and return the square of that number

#include <stdio.h> #include <stdlib.h> float binhphuong(float r); int main(int argc, char *argv[]) { float r; printf("so thuc r = "); scanf("%f", &r); float a = binhphuong(r); printf("%.2f", a); }

float binhphuong(float r) { float a = r*r; return a; }


2. Write a C program to find the area and perimeter of a circle.
Function prototype: float areaCircle(float radius);
Function prototype: float perimeterCircle(float radius);


Using the library: “Math.h” => PI = 3.14

#include <stdio.h> #include <stdlib.h> float dientich(float r, float y); int main(int argc, char *argv[]) { float r; float y = 3.14; printf("ban kinh r = "); scanf("%f", &r); float a = dientich(r,y); printf("%.2f", a); } float dientich(float r, float y) { float a = r*r*3.14; return a; }


3. Write a C program to calculate the factorial of an integer
Function prototype:  long factorial(int number);

#include <stdio.h>
#include <stdlib.h>
int giaithua(int r);

int main(int argc, char *argv[]) {
int r;
printf("so nguyen r = ");
scanf("%d", &r);
long a = giaithua(r);
printf("%ld", a);
}

int giaithua(int r)
{
long a = 1;
while(r > 0)
{
a = a*r;
r = r - 1;
}
return a;
}


4. Write a program with a function that takes two int parameters, adds them together, then returns the sum. The program should ask the user for two numbers, then call the function with the numbers as arguments, and tell the user the sum.
Function prototype: int calSum(int number1, int number2);

#include <stdio.h>
#include <stdlib.h>
int sum(int a, int b);

int main(int argc, char *argv[]) {
int a;
int b;
printf("so nguyen a = ");
scanf("%d", &a);
printf("so nguyen b = ");
scanf("%d", &b);
int c = sum(a,b);
printf("%d" ,c);
}

int sum(int a, int b)
{
int c;
c = a + b;

return c;

}

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;
}

Day 4 (While loop)

                                                  Vòng lặp While
I.Khái niệm
-Như đã đề cập ở bài "vòng lặp for", vòng lặp while cũng là hàm sử dụng phổ biến trong môn C
-Nhưng khác là vòng lặp For có biểu thức liện hệ cho biến về vòng lặp được xác định cụ thể hơn thì vòng lặp While không có, máy tính sẽ chỉ nhập giá trị đúng hoặc sai, và nếu được gán giá trị đúng, máy tính sẽ thực hiện đến khi nào sai thì thôi.
II.Cấu trúc
-Cấu trúc while như sau :
             While(điều kiện)
              {
                       câu lệnh;
               }

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; while(x<=18) { printf("\nDuong Quan : 18 tuoi",x); x=x+1; } 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; while(x<=30) { printf("\n%d",x); x=x+2; } 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; while(x>=0) { printf("% d",x); x=x-1; } 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,y=1,n=1; printf("nhap mot so:"); scanf("%d",&x); while(x>=y) { n=n*y; y++; } printf("luy thua cua %d la:%d",x,n); 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,n=1,gt=1; printf("nhap 1 so:"); scanf("%d",&a); while(n<=a) { gt=gt*n; n++; } printf("giai thua cua %d la:%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; while(x>=5) { printf("% d",x); x=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> 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; while ((min<x) && (x<max)) { if (x%2 != 0) { sum=sum+x; } x++; } printf("tong cac so le giua 2 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; while(x<10) { sum1=sum2; sum2=sum3; sum3=sum1+sum2; x++; printf("\n%d",sum1); } return 0; }

Thứ Hai, 2 tháng 2, 2015

Day 4 (for loop)

                                    Cấu trúc vòng lặp (loop) For
I.Khái niệm
-Vòng lặp là chuỗi code thực hiện các lệnh giống nhau với số lượng xác định, cho tới khi điều kiện không còn thỏa mãn, thì vòng lặp vẫn tiếp tục thực hiện lệnh.
-Trong môn C thì ta sẽ học 3 loại vòng lặp:
   +vòng lặp for
   +vòng lặp while
   +vòng lặp do...while.
-Điều kiện dùng đề cho vòng lặp tiếp tục thực hiện thì thể hiện "Mối quan hệ" và "Phép logic" giữa các giá trị biến.
II.Vòng lặp For
- Cấu trúc vòng lặp For về cơ bản như sau:

     For(Biến đếm ; Điều kiện ; Biểu thức đánh giá);
{
     Câu lệnh;
}

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 i; int y; char YourName[20]; printf("Your age = "); scanf("%d", &i); printf("Your name is "); fflush(stdin); gets(YourName); for(y = 1; y <= i ; y = y+1) { printf("\n%s", YourName); } 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 i = 1; for(;i <= 30 ; i = i+1) { if(i % 2 ==0) printf("\n%d", i); } 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 i = 10; for(; i>= 0 ; i = i-1) { if(i >= 0 && i<= 10) printf("\n%d", i); } 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; for(x=10;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 x=10,y=1;
printf("nhap vao mot so:");
scanf("%d",&x);
for(x;x>1;x=x-1){
y=y*x;
}
printf("luy thua cua %d la:%d",x,y);
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 i = 100; for(; i>= 5 ; i = i-5) { if(i % 5 ==0) printf("\n%d", i); } return 0; }

**Bài 7**:  Accept two numbers num1 and num2. Find the sum of all odd numbers between the two
 numbers entered

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;
for(x;(min<x) && (x<max);x++){
if (x%2 !=0) {
sum=sum+x; }
}
printf("the sum of all odd numbers between the two numbers entered: %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; for(x;x<10;x++) { sum1=sum2; sum2=sum3; sum3=sum1+sum2; printf("\n%d",sum1); } return 0; }