Thứ Hai, 9 tháng 2, 2015

Hàm - chương trình con - function in C

Hàm - chương trình con -  function in C

- chương trình và code sẵn Tại Đây




1. Write a C program that accepts a number and square the number with the help of a function.To do this,


#include <stdio.h>
#include <stdlib.h>
/* Write a C program that accepts a number and square the number with the help of a function.
To do this */
int BP (int);
int main(int argc, char *argv[]) {
 printf ("vao x = ");
 int x;
 scanf("%d",&x);
 printf("binh phuong cua x la y = %d", BP(x)); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
int BP (int x) {
 int y = x*x;
 return (y);
}

binh-phuong
bình phương


2. Write a C program to find the area and perimeter of a circle.


#include <stdio.h>
#include <stdlib.h>
/* 2. Write a C program to find the area and perimeter of a circle. */
float c(float);
float v(float);
int main(int argc, char *argv[]) {
 printf ("ban kinh r = ");
 float r;
 scanf("%f",&r);
 printf(" chu vi la : %f \n the tich la : %f",c(r),v(r)); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
float c(float x){
 float y = 2*x*3.14159265359;
 return y;
}
float v(float x){
 float y = x*x*3.14159265359;
 return y;
}


chu-vi-the-tich-hinh-tron
chu vi thể tính hình tròn





3. Write a C program to calculate the factorial of an integer


#include <stdio.h>
#include <stdlib.h>
/* 3. Write a C program to calculate the factorial of an integer */
int g(int);
int main(int argc, char *argv[]) {
 printf("vao so nguyen X = ");
 int x;
 scanf("%d",&x);
 printf ("%d! = %d",x,g(x)); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
int g(int x){
 int y = 1;
 for (;x>0;x--) y=y*x;
 return y;
}

tim-giai-thua
tìm giai thừ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.


#include <stdio.h>
#include <stdlib.h>
/* 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 */
int s(int,int);
int main(int argc, char *argv[]) {
 int x,y;
 printf ("vao x = ");
 scanf ("%d",&x);
 printf ("vao y = ");
 scanf ("%d",&y);
 printf("%d + %d = %d ",x,y,s(x,y)); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
int s(int x,int y){
 int z = x+y;
 return z;
}

tinh-tong
tính tông



end
















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

Đăng nhận xét