作业p39

/*编程,计算两数字的和差积商余*/
#include<stdio.h> int main(void) { int num1,num2,sum,difference,product,quotient,remainder; /**/
printf(
"Enter num1:"); scanf("%d",&num1); printf("Enter num2:"); scanf("%d",&num2); sum=num1+num2; printf("sum=%d\n",sum); /*计算两数之和*/
difference
=num1-num2; printf("difference=%d\n",difference);
/*计算两数之差*/ product
=num1*num2; printf("product=%d\n",product); /*计算两数乘积*/
quotient
=num1/num2; printf("quotient=%d\n",quotient); /*计算两数之商*/
remainder
=num1%num2; printf("remainder=%d\n",remainder); /*计算两数之余*/
return 0; }

/*电费*/
#include<stdio.h> int main(void) { double x,y; printf("Enter x(x>=0):\n"); scanf("%lf",&x); if(x<=50){ y=0.53*x/3; } else{ y=0.58*x-2.5; } printf("y=f(%f)=%.2f\n",x,y); return 0; }
/*实验中有*/

/*计算x的n次方*/
#include<stdio.h> int main(void) { int a,x,n,i; printf("Enter x:"); scanf("%d",&x); printf("Enter n:"); scanf("%d",&n); a=x; for(i=1;i<n;i++) { x=x*a; } /*进入循环,计算x的n次方*/
printf(
"Enter result:"); printf("%d\n",x); return 0; }


/*输入两个正整数m,n,求(i^2+1/i),从m到n的和*/ 
#include<stdio.h> int main(void) { int m,n,i; double product,result; /*result为单个元素的解,product为所有解的和*/
printf(
"Enter m:"); scanf("%d",&m); printf("Enter n:"); scanf("%d",&n); product=0; /*从m到n依次循环求解*/
for(i=m;i<=n;i++){ result=(i*i)+(1/i); printf("%d",result); /*将单个元素所求解累加起来*/
product
=product+result; } printf("product=%.2f\n",product); return 0; }

 

/*输入一个正整数n,计算1-(2/3)+(3/5)-(4/7)+(5/9)-...的前n项和*/
#include<stdio.h>
int main(void)
{
   
int i,n,numerator,denominator,flag;
   
double sum,item;

    printf(
"Enter n:");
    scanf(
"%d",&n);
    numerator
=1;
    denominator
=1;
    sum
=0;
    flag
=1;

/*循环n次求解各项并相加求和*/   
   
for(i=1;i<=n;i++){
        item
=flag*(numerator*1.0/denominator);
        numerator
=numerator+1;
        denominator
=denominator+2;
        flag
=-flag;
        sum
=sum+item;
    }
    printf(
"sum=%lf\n",sum);

   
return 0;
}

/*输入正整数n,求2^1+2^2+……+2^n*/
#include<stdio.h> #include<math.h> int main(void) { int n,i,power; double product; printf("Enter n:"); scanf("%d",&n); product=0; for(i=1;i<=n;i++){/*进入循环*/ power=pow(2,i);/*计算2的n次方*/
printf(
"%d",power)
product
=product+power;/*求和*/
} printf(
"product=%.0f\n",product); return 0; }

#include<stdio.h>
double fact(int n);
int main(void)
{
    int i,n;
    double sum;

    printf("Enter n:");
    scanf("%d",&n);
    sum=0;
    for(i=0;i<=n;i++){
        sum=sum+fact(i);
        }
printf(
"%d",sum); return 0; } double fact(int n) { int i; double product; product=1; for(i=1;i<=n;i++){ product=product*i;
}
return product; }

#include<stdio.h>
int main(void)
{
    int x,y;
    printf("输入x:");
    scanf("%d",&x);

    if(x<0){
        y=-1;
    }
    else if(x=0){
        y=0;
    }
    else{
        y=1;
    }
    printf("y=%d",y);
    return 0;
}

#include<stdio.h>
int main(void)
{
    int count,num,n;
    double grade,total;

    num=0;
    total=0;
    count=0;
    printf("Enter n:");
    scanf("%d",&n);
    printf("Enter grades:");
    scanf("%lf",&grade);

    while(grade>=0){
        total=total+grade;
        num++;
        if(grade>60)
            count++;
        scanf("%lf",&grade);
    }
    if(num!=0){
        printf("Grade average is%.2f\n",total/num);
        printf("Number of success is %d\n",count);
    }
    else
        printf("Grade average is0\n");

    return 0;
}

#include<stdio.h>
int main(void)
{
    int digit,letter,blank,other;
    char ch;

    int i;
    digit=letter=other=blank=0;
    printf("Enter 15 characters:");
    for(i=1;i<=15;i++){
        ch=getchar();
        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
            letter++;
        else if(ch>='0'&&ch<='9')
            digit++;
        else if(ch>=' '||ch<='\r')
            blank++;
        else
            other++;
    }
    printf("letter=%d,digit=%d,blank=%d\n,other=%d\n",letter,digit,blank,other);


    return 0;
}

#include<stdio.h>
int main(void)
{
    int i,year,n;    
    printf("Enter n:");
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        printf("Enter year:");
    scanf("%d",&year);
     if((year%4==0&&year%100!=0)||(year%400==0)){
        printf("Right!\n");
    }
    else{
        printf("Wrong!\n");
    }
    }
    return 0;
    }

#include<stdio.h>
int main(void)
{
    int repeat;
    char ch;


    printf("Enter repeat:");
    scanf("%d",&repeat);
    ch = getchar();
    switch(ch){
    case'A':
        printf("grade=90~100\n");
        break;
    case'B':
        printf("grade=80~89\n");
        break;
    case'C':
        printf("grade=70~79\n");
        break;    
    case'D':
        printf("grade=60~69\n");
        break;
    case'E':
        printf("grade=0~59\n");
        break;
    default:
        printf("Unknown operator\n");
        break;
    }


    return 0;
}

#include<stdio.h>
int main(void)
{
    int choice,i;
    double price;


    for(i=1;i<=5;i++){
        printf("[1]Select apple \n");
        printf("[2]Select rears \n");
        printf("[3]Select oranges \n");
        printf("[4]Select grapes \n");
        printf("[0]exit \n");


        printf("Enter choice:");
        scanf("%d",&choice);


        if(choice==0)
            break;
        switch(choice){
            case 1:price=3.00;break;
            case 2:price=2.50;break;
            case 3:price=4.10;break;
            case 4:price=10.20;break;
            default:price=0.00;break;
        }
        printf("price=%0.2f\n",price);
    }


    printf("Thanks \n");


    return 0;
}

posted on 2013-10-19 23:39  yy99  阅读(280)  评论(0编辑  收藏  举报