/*计算和,差,商,积*/
#include<stdio.h> int main(void) { double num1,num2; char op; printf("Type in an expression:"); scanf("%lf%c%lf",&num1,&op,&num2); switch(op){ case'+': printf("=%.2f\n",num1+num2); break; case'-': printf("=%.2f\n",num1-num2); break; case'*': printf("=%.2f\n",num1*num2); break; case'/': printf("=%.2f\n",num1/num2); break; default: printf("SORRY,sorry\n"); break; } return 0; }
/*输入x,n,计算x^n*/
#include<stdio.h> int main(void) { int i,n; double sum, x; printf("enter x,n:"); scanf("%lf%d", &x, &n); sum= 1; for(i = 1; i <= n; i++) { sum= sum * x; printf("%.2f\n", sum); } return 0; }
/*计算i^2+1/i的和*/
#include <stdio.h> int main(void) { int i,m,n; double sum; printf("enter m and n:"); scanf("%d%d",&m,&n); sum=0; /*赋予sum的初值为0*/
for(i=m;i<=n;i++){ sum=sum+i*i+1.0/i; } printf("sum=%.2f\n",sum); return 0; }
/*计算1-2/3+3/5-4/7+5/9-6/11+...的n项之和*/
#include<stdio.h> #include<math.h> int main(void) { int i,n,fenzi,fenmu,flag; double item,sum; printf("enter n:"); scanf("%d",&n); flag=1; /*赋予初值*/
fenzi=1; fenmu=1; sum=0; for(i=1;i<=n;i++){ item=1.0*flag*fenzi/fenmu; sum=sum+item; fenzi=fenzi+1; fenmu=fenmu+2; flag=-flag; } printf("sum=%.2f\n",sum); return 0; }
/*输出“还款年限-月还款表”*/
#include <stdio.h> #include <math.h> /*程序中调用数学函数*/
int main(void) { int year; double loan,money,my,rate; scanf("%Lf",&loan); scanf("%Lf",&rate); printf("enter year money\n"); for(year=5;year<=30;year++){ my=pow(1+rate,12*year); money=loan*rate*my/(my-1); printf("year=%d money=%.0f\n",year,money); } return 0; }
/*调用pow函数求幂*/
#include<stdio.h> #include<math.h> /*调用数学函数*/
int main(void) { int n,i; double power,sum; printf("enter n:"); scanf("%d",&n); sum=0; for(i=1;i<=n;i++){ power=pow(2,i); /*调用幂函数pow(2,i)计算2的i次方*/
sum=sum+power; } printf("sum=%.2f\n",sum); return 0; }