#include<stdio.h>
#define N 10
void main()
{
int a[N], i,s1=0,count=0;
for(i=0;i<N;i++)
scanf("%d",&a[i]);
for(i=0;i<N;i++)
{
if(a[i]>0)
{
s1+=a[i];
count++;
}
}
printf("所有正數(shù)的和:%d\n",s1);
}
輸入三角形三條邊長時:
#include<stdio.h>
#include<math.h>
void main()
{
double a,b,c,t,s;
printf("請輸入表示三角形三邊長的三個實數(shù)\n");
scanf("%lf%lf%lf", &a, &b, &c);
if( a+b>c && a+c>b && b+c>a )//判斷是否構(gòu)成三角形:條件,任意兩邊之和大于第三邊
{
t=(a+b+c)/2;
s=sqrt( t*(t-a)*(t-b)*(t-c) );//三角形面積與三邊長之間的關(guān)系
printf("三邊:a=%.2lf b=%.2lf c=%.2lf 對應(yīng)三角形面積為:%.2lf\n", a,b,c,s );
}else
printf("a=%.2lf b=%.2lf c=%.2lf\n不能構(gòu)成三角形\n\n", a,b,c );
}
輸入3個整數(shù):
#include<stdio.h>
main void()
{
int a,b,c,t;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
t=a;
a=b;
b=t;
}
if(b>c)
{
t=b;
b=c;
c=t;
}
if(a<c)
{
t=a;
a=c;
c=t;
}
printf("%d",a);
}
在1-500范圍內(nèi),找出所有同時滿足用3除余2,用5除余3,用 7除余 4的所有整數(shù):
#include <stdio.h>
void main()
{
int i;
for(i=11;i<=494;i++)
if(i%3==2&&i%5==3&&i%7==4)
printf(" %d ",i);
}
計算數(shù)列1/2,2/3,3/5,5/8,8/13,……的前20項和:
#include<stdio.h>
void main()
{
int i,m=1,n=2,k;
float sum=0,a[20];
for(i=0;i<20;i++)
{
k=n;
a[i]=(float)m/n;
n+=m;
m=k;
}
k=0;
for(i=0;i<20;i++) sum+=a[i];
printf("前二十個數(shù)為:\n");
for(i=0;i<20;i++)
{
k++;
printf("%f ",a[i]);
if(k==5)
{
printf("\n");
k=0;
}
}
printf("\n他們之和為:%f\n",sum);
}