/*编程,计算两数字的和差积商余*/
#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
",sum);
/*计算两数之和*/
difference=num1-num2;
printf("difference=%d
",difference);
/*计算两数之差*/
product=num1*num2;
printf("product=%d
",product);
/*计算两数乘积*/
quotient=num1/num2;
printf("quotient=%d
",quotient);
/*计算两数之商*/
remainder=num1%num2;
printf("remainder=%d
",remainder);
/*计算两数之余*/
return 0;
}
/*电费*/
#include<stdio.h>
int main(void)
{
double x,y;
printf("Enter x(x>=0):
");
scanf("%lf",&x);
if(x<=50){
y=0.53*x/3;
}
else{
y=0.58*x-2.5;
}
printf("y=f(%f)=%.2f
",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
",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
",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
",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
",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
",total/num);
printf("Number of success is %d
",count);
}
else
printf("Grade average is0
");
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<='
')
blank++;
else
other++;
}
printf("letter=%d,digit=%d,blank=%d
,other=%d
",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!
");
}
else{
printf("Wrong!
");
}
}
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
");
break;
case'B':
printf("grade=80~89
");
break;
case'C':
printf("grade=70~79
");
break;
case'D':
printf("grade=60~69
");
break;
case'E':
printf("grade=0~59
");
break;
default:
printf("Unknown operator
");
break;
}
return 0;
}
#include<stdio.h>
int main(void)
{
int choice,i;
double price;
for(i=1;i<=5;i++){
printf("[1]Select apple
");
printf("[2]Select rears
");
printf("[3]Select oranges
");
printf("[4]Select grapes
");
printf("[0]exit
");
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
",price);
}
printf("Thanks
");
return 0;
}