1.
#include<stdio.h> int main(void) { int num1,num2; char op; /*自定义一个字符串类型的变量*/ printf("Type in an expression:"); /*提示输入一个算式*/ scanf("%d%c%d",&num1,&op,&num2); /*输入一个整数,一个字符,一个整数*/ if(op=='+') /* 判断字符变量是否为+,若是则进行下行操作。下同 */ printf("=%d\n",num1+num2); else if(op=='-') printf("=%d\n",num1-num2); else if(op=='*') printf("=%d\n",num1*num2); else if(op=='/') printf("=%d\n",num1/num2); else if(op=='%') printf("=%d\n",num1%num2); else printf("Unknown operator\n"); return 0; }
3.
#include<stdio.h> int main(void) { int i,n; double x,y; y=1; printf("Enter x and n:"); scanf("%Lf%d",&x,&n); for(i=1;i<=n;i++){ y=y*x; /*计算x乘以本身n次*/ } printf("y=%.2f",y); return 0; }
4.
#include<stdio.h> int main(void) { int m,n,i; double y; y=0; printf("Enter m and n:"); scanf("%d%d",&m,&n); for(i=m;i<=n;i++){ y=y+i*i+1/i; } printf("y=%.2f\n",y); return 0; }
5.
#include<stdio.h> int main(void) { int i,n,x; double y; x=1; y=0; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++){ y=y+(x*i)/(2*i-1); x=-x; } printf("y=%.2f\n",y); return 0; }
为神马运行出来结果永远是1?!到底错哪里了?!!!T^T。啊啊啊啊啊桑心死了。我真的要哭了啦!不管了先继续做下去了T T。
6.
#include<stdio.h> #include<math.h> int main(void) { int year; double rate,money,loan,x; printf("Enter loan & rate:"); scanf("%LfLf",&loan,&rate); for(year=5;year<=30;year++){ x=pow(1+rate,12*year); money=loan*rate*x/(x-1); printf("year=%d,money=%.2f\n",year,money); } return 0; }
7.
#include<stdio.h> #include<math.h> int main(void) { int n,i,x; double y; printf("Enter n:"); scanf("%d",&n); y=0; for(i=1;i<=n;i++){ x=pow(2,i); y=y+x; } printf("y=%.2f\n",y); return 0; }