Hiển thị các bài đăng có nhãn ham. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn ham. Hiển thị tất cả bài đăng

Thứ Hai, 9 tháng 2, 2015

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

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

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




#include <stdio.h>
#include <stdlib.h>
void s (int *x, int *y);
int main(int argc, char *argv[]) {
 printf (" vao 2 so nguyen : ");
 int x,y;
 scanf("%d%d",&x,&y);
 s(&x,&y);
 printf("2 so sau khi hoan vi la : %d  %d",x,y); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
void s (int *x, int *y){
 int z = *x;
 *x = *y;
 *y = z;
}


hoan-vi
Hoán vị






#include <stdio.h>
#include <stdlib.h>
void r (int a[], int n);
int main(int argc, char *argv[]) {
 printf("nhap vao do dai mang: ");
 int i,n;
 scanf("%d",&n);
 int a[100];
 for (i=0;i<n;i++) {
  printf ("A[%d]= ",i+1);
  scanf ("%d",&a[i]);
 } 
 r (a,n); 
 printf("chuoi sau khi dao nguoc la: ");
 for (i=0;i<n;i++) printf ("%d ",a[i]); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
void r (int a[100], int n){
 int i,j;
 for (i=0;i<n/2;i++){
  j=a[i];
  a[i] = a[n-1-i];
  a[n-1-i]=j;
 }
}
Dao-nguoc
Đảo ngược






#include <stdio.h>
#include <stdlib.h>
int c(char a[], char i);
int main(int argc, char *argv[]) {
 char a[100],b;
 printf("vao chuoi ki tu A: ");
 gets(a);
 printf("vao ki tu B: ");
 gets(&b);
 printf("\n\n\n");
 c(a,b);
 printf("\n\n\n");
 system("pause");
 return 0;
}
int c(char a[100], char b){
 int i,t=0;
 for (i=0;i<100;i++) if (a[i]== b) t=t+1;
 if (t==0) printf("-1"); 
 if (t!=0) printf("%d",t);
}


tim-kiem
tìm kiếm




end

Hàm - chương trình con - function in C (tiếp)


Hàm - chương trình con - function in C (tiếp)



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

1. Write a function which accepts an integer value as input, and returns an integer which is the cube of that input


#include <stdio.h>
#include <stdlib.h>

/* Write a function which accepts an integer value as input, and returns an integer which is the cube of that input */
int l(int);
int main(int argc, char *argv[]) {
 printf("vao X= ");
 int x;
 scanf("%d",&x);
 printf("x^3 = %d",l(x)); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
int l(int x){
 int y = x*x*x;
 return y;
}

lap-phuong
Lập phương


2. Write a function that accepts a char as input and returns true if the char is a digit from 0 to 9 or false if the character is not a digit from 0 to 9



#include <stdio.h>
#include <stdlib.h>
/* Write a function that accepts a char as input and returns true 
if the char is a digit from 0 to 9 or false if the character is not a digit from 0 to 9 */
int tf(char);
int main(int argc, char *argv[]) {
 printf("vao ki tu : ");
 char x;
 scanf("%c",&x);
 if (tf(x)) printf("%c nam giua 0 - 9",x);
  else printf ("%c khong nam giua 0 - 9",x); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
int tf(char x){
 int y;
 if (x >= '0' && x<= '9') y = 1;
 else y = 0;
 return y;
}

tim-kiem
tìm kiếm



#include <stdio.h>
#include <stdlib.h>
/* . Write a function named Smallest that takes three integer inputs 
and returns an integer that is the smallest of the three inputs.  
Write the prototype for the Smallest function.  
Write  a program that gets 3 integers from a user and displays the smallest. */
int m(int,int,int);
int main(int argc, char *argv[]) {
 printf ("vao 3 so : ");
 int x,y,z;
 scanf("%d%d%d",&x,&y,&z);
 printf(" so nho nhat trong 3 so %d %d %d la : %d",x,y,z,m(x,y,z)); 
 printf("\n\n\n");
 system("pause");
 return 0;
}
int m(int x,int y, int z){
 int i =x;
 if (i > y) i = y;
 if (i > z) i = z;
 return i;
}

min
min


4. Write a function that, given a letter of the alphabet, returns true if the letter is a vowel (lower or uppercase) and returns false if the letter is not a vowel.



#include <stdio.h>
#include <stdlib.h>
/* Write a function that, given a letter of the alphabet, 
returns true if the letter is a vowel (lower or uppercase) and returns false if the letter is not a vowel. */
int t(char);
int main(int argc, char *argv[]) {
 printf ("vao 1 chu: ");
 char x;
 gets(&x);
 if (t(x)) printf ("%c la nguyen am",x);
  else printf ("%c khong phai la nguyen am",x);
 printf("\n\n\n");
 system("pause");
 return 0;
}
int t(char x){
 int t;
 if (x=='u'||x=='U'||x=='e'||x=='E'||x=='o'||x=='O'||x=='a'||x=='A'||x=='i'||x=='I') t= 1;
  else t=0;
 return t;
}


tim-nguyen-am
tim nguyên âm


end






















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