1.
#include
double min(double a, double b);
int main(void)
{
double x,y;
double mini;
printf("Enter two numbers:");
scanf("%lf%lf",&x,&y);
mini = min(x,y);
printf("The minimum of the two numbers is %lf",mini);
return 0;
}
double min(double a, double b)
{
return((a > b)? b : a);
}
2.
#include
void chline(char,int,int);
int main(void)
{
char ch;
int i,j;
printf("Enter the char:");
scanf("%c",&ch);
printf("Enter two numbers for lines and rows:");
scanf("%d%d",&i,&j);
chline(ch,i,j);
return 0;
}
void chline(char ch,int i,int j)
{
int a,b;
for(a = 0; a < j; a++)
{
for(b = 0; b < i; b++)
printf("%c",ch);
printf("\n");
}
}
3.
#include
void print(char,int,int);
int main(void)
{
char ch;
int i,j;
scanf("%c",&ch);
scanf("%d%d",&i,&j);
print(ch,i,j);
return 0;
}
void print(char ch, int i, int j)
{
int a,b;
for(a = 0;a < j;a++)
{
for(b = 0;b < i;b++)
printf("%c",ch);
printf("\n");
}
}
4.
#include
double harmonic(double,double);
int main(void)
{
double a,b;
double harmo;
printf("Enter two numbers:");
scanf("%lf%lf",&a,&b);
harmo = harmonic(a,b);
printf("The harmonic mean of the two numbers is %lf\n",harmo);
return 0;
}
double harmonic(double a,double b)
{
a = 1 / a;
b = 1 / b;
return (1 / ((a + b) / 2));
}
5.
#include
double larger_of(double *,double *);
int main(void)
{
double x,y;
scanf("%lf%lf",&x,&y);
larger_of(&x,&y);
printf("The larger of x and y is %lf, %lf",x,y);
return 0;
}
double larger_of(double *a,double *b)
{
*a = (*a > *b)? *a : *b;
*b = *a;
}
6.
#include
void sort(double *,double *,double *);
int main(void)
{
double x,y,z;
printf("Enter three numbers, I will sort them for you:");
scanf("%lf%lf%lf",&x,&y,&z);
sort(&x,&y,&z);
printf("From big to small is %lf %lf %lf.\n",z,y,x);
}
void sort(double *a,double *b,double *c)
{
double temp;
if(*a > *b) //如果x大于y,则交换x和y,使x较小
{
temp = *a;
*a = *b;
*b = temp;
}
if(*a > *c) //如果x大于z,则交换x和z,使x较小
{
temp = *a;
*a = *c;
*c = temp; //经过这两个if,x已经变成了最小的值,之后只需再比较y和z,然后排序即可
}
if(*b > *c) //如果y大于z,则交换y和z,使y比z小,此时x
7.
#include
#include
int letter(void);
int main(void)
{
letter();
return 0;
}
int letter(void)
{
char let;
int num;
printf("Enter some words:");
while((let = getchar()) != EOF)
{
if(isalpha(let))
{
num = tolower(let) - 'a' + 1;
putchar(let);
printf(" is a letter,");
printf(" and it is at %d seat.\n",num);
}
else
{
if(let == '\n')
{
printf("\\n is not a letter.\n");
}
else
{
putchar(let);
printf(" is not a letter.\n");
}
}
}
}
8.
#include
double power(double n,int p);
int main(void)
{
double x, xpow;
int exp;
while(scanf("%lf%d",&x,&exp) == 2)
{
xpow = power(x,exp);
if (x == 0)
printf("Undefined, but result is ");
printf("%.5g\n",xpow);
}
return 0;
}
double power(double n,int p)
{
double pow = 1;
double pow1 = 1;
int i;
if(n == 0)
{
pow = 1;
}
else
{
if (p > 0)
{
for(i = 1; i <= p; i++)
pow *= n;
}
else if(p < 0)
{
for(i = 1;i <= -p; i++)
pow1 *= n;
pow = 1 / pow1;
}
else
pow = 1;
}
return pow;
}
9.
#include
double power(double n,int p);
int main(void)
{
double x, xpow;
int exp;
while(scanf("%lf%d",&x,&exp) == 2)
{
xpow = power(x,exp);
if (x == 0)
printf("Undefined, but result is ");
printf("%.5g\n",xpow);
}
return 0;
}
double power(double n,int p)
{
double pow;
double pow1;
int i;
if(p > 0)
{
if(p > 1)
pow = n * power(n,p - 1);
else
pow = n;
}
else if(p < 0)
{
if(p < -1)
pow1 = n * power(n,-p - 1);
else
pow1 = n;
pow = 1 / pow1;
}
else
pow = 1;
return pow;
}
10.
#include
void to_base_n(int,int);
int main(void)
{
int a,b;
scanf("%d%d",&a,&b);
to_base_n(a,b);
return 0;
}
void to_base_n(int a,int b)
{
int left;
left = a % b;
if((a / b) > 0)
{
to_base_n(a / b, b);
}
else;
printf("%d",left);
}
11.
#include
unsigned long Fibonacci(int);
int main(void)
{
int n;
unsigned long fibo;
printf("Enter the location of Fibonacci:");
scanf("%d",&n);
fibo = Fibonacci(n);
printf("The value is %lld\n",fibo);
return 0;
}
unsigned long Fibonacci(int n)
{
int i,n1,n2,temp;
if(n > 2)
{
for(n1 = 1, n2 = 1, i = 3; i <= n ; i++)
{
temp = n1 + n2;
n1 = n2;
n2 = temp;
}
}
else
n2 = 1;
return n2;
}