M=(㏒P-㏒(P-D*R))/ ㏒(1+R)
M是還清貸款所需月數。今假設D=324 500元,P=3245元,R=0.8%。編程序求還貸月數M和總***要還多少錢。
#include <math.h>
#include <stdio.h>
double M(double P, double D, double R)
{
double a, b, c;
a = log(P);
b = log(P - D*R);
c = log(1 + R);
return (a - b)/c;
}
int main(void)
{
double d = 324500, p = 3245, r = .008f;
long month = 0;
month = (long)M(p,d,r);
printf("months = %d\npayment = %d\n", month, month * (long)p);
return 0;
}
/*運行結果:
months = 201
payment = 652245
*/
2、編寫程序,逐個輸出英文字母C,H,I,N,A。然後按反序輸出,既A,N,I,H,C。
#include <stdio.h>
int main(void)
{
char s[6]="CHINA"; int i = 0;
for(i = 0; i < 5; i++) printf("%c ", s[i]);
printf("\n");
for(i = 4; i > -1; i--)printf("%c ", s[i]);
printf("\n");
return 0;
}
3、輸入三角形的三邊長a,b,c,編寫程序求三角形面積area。已知三角形面積公式為:
area=sprt(s(s-a)(s-b)(s-c)),其中s=(a+b+c)/2。
#include <math.h>
#include <stdio.h>
double area(double a, double b, double c)
{
double s = 0;
s = (a + b + c)/2.0f;
s = s * (s - a) * (s - b) * (s - c);
return sqrt(s);
}
int main(void)
{
double a, b, c;
scanf("%f %f %f", &a, &b, &c);
printf("area = %f", area(a, b, c));
return 0;
}
4、編寫程序,求ax ?+bx+c=0方程的根。a,b,c由鍵盤輸入,設b ?-4ac>0。
#include <math.h>
#include <stdio.h>
double area(double a, double b, double c)
{
double s = 0;
s = (a + b + c)/2.0f;
s = s * (s - a) * (s - b) * (s - c);
return sqrt(s);
}
int main(void)
{
int a, b, c, d; double e, x, y;
scanf("%d %d %d", &a, &b, &c);
d = b * b - 4 * a * c;
if(d < 0) {
printf("NO REAL ROOTS.\n");
return 0;
}
if(d == 0) {
e = -2 * a;
e = (double)b / e;
printf("X1 = X2 = %f", e);
return 0;
}
e = d;
e = sqrt(e);
x = (-(double)b + e) / (double)(2 * a);
y = (-(double)b - e) / (double)(2 * a);
printf("X1 = %f, X2 = %f\n", x, y);
return 0;
}