分支和循环
1.
#include<stdio.h> int main(void) { double num1,num2; char op; printf("Type in an expression:"); scanf("%lf%c%lf",&num1,&op,&num2); if(op=='+') printf("=%.2f\n",num1+num2); else if(op=='-') printf("=%.2f\n",num1-num2); else if(op=='*') printf("=%.2f\n",num1*num2); else if(op=='/') printf("=%.2f\n",num1/num2); else printf("Unknown operator\n"); return 0; }
2.
#include<stdio.h> int main(void) { double x,y; printf("请输入电量:\n"); scanf("%Lf",&x); if(x<0){ printf("sorry,erro"); } else if(x<=50){ y=0.53*x; } else{ y=50*0.53+(x-50)*0.58; } printf("y=%.3f\n",y); return 0; }
3.
#include <stdio.h> int main() { double x,pow = 1.0; int i,n; printf("Enter x and n:\n"); scanf("%Lf%d",&x,&n); for(i=1;i<=n;i++) pow=pow*x; printf("pow(%Lf%d)=%.2f\n",x,n,pow); return 0; }
4.
#include<stdio.h> int main(void) { int m,n,i; double y; y=0; printf("Enter m,n:"); scanf("%d%d",&m,&n); for(i=m;i<=n;i++) y=y+(i*i+1.0/i); printf("y=%Lf\n",y); }
5.
#include<stdio.h> int main(void) { int i,n; double item,sum,flag; flag =1; sum =0; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++){ item=(flag*i)/(2*i-1); flag=-flag; sum=sum+item; } printf("sum=%f\n",sum); }
6.
#include<stdio.h> #include<math.h> int main(void) { int month,year,loan,lower,money,upper; double rate; lower=5; upper=30; printf("Enter loan:"); scanf("%d",&loan); printf("Enter rate:"); scanf("%Lf",&rate); printf("Enter year:"); for(year=lower;year<=upper;year++){ month=year*12; money=(loan*rate*pow((1+rate),month))/(pow((1+rate),month)-1); printf("%d: %d\n",year,money); } return 0; }
7.
#include<stdio.h> #include<math.h> int main(void) { int n,i; double sum,item; sum=0; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++){ item=powl(2,i); sum=sum+item; } printf("sum=%Lf\n",sum); return 0; }