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


























