比较四个正整数大小:
#include<stdio.h>
void main()
{
int a,b,c,d,t;
printf("请输入4个数:");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
if(a>b)
{
t=a;
a=b;
b=t;
}
if(a>c)
{
t=a;
a=c;
c=t;
}
if(a>d)
{
t=a;
a=d;
d=t;
}
if(b>c)
{
t=b;
b=c;
c=d;
}
if(b>d)
{
t=b;
b=d;
d=t;
}
if(c>d)
{
t=c;
c=d;
d=t;
}
printf("输出4个数:");
printf("%d %d %d %d",a,b,c,d);
}
求1加到100:
while:
#include <stdio.h>
void main()
{
int i,sum=0;
i=1;
while(i<=100)
{
sum=sum+i;
i++;
}
printf("%d\n",sum);
}
do...while:
#include <stdio.h>
void main()
{
int i,sum=0;
i=1;
do
{
sum=sum+i;
i=i++;
}
while(i<=100);
printf("%d\n",sum);
}
for:
#include <stdio.h>
void main()
{
int i,sum=0;
for(i=1;i<=100;i=i++)
sum=sum+i;
printf("%d\n",sum);
}
判断大小写 如大转小:
#include <stdio.h>
void main()
{
char ch;
scanf("%c",&ch);
ch=(ch>='A'&&ch<='Z')? (ch+32):ch;
printf("%c\n",ch);
}
判断是否闰年:
int year,leap;
scanf("%d",&year);
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
}
else
leap=0;
if(leap)
printf("%d is",year);
else
printf("%d is not",year);
printf(" a leap year.\n");
}
求3到一百的素数:
#include<stdio.h>
int shushuo(int a);
void main()
{
int i;
int flag;
int b=0;
for(i=3;i<=100;i++)
{
flag=shushuo(i);
if(flag==1)
{ b++;
printf("%d\n",i);
}
}
printf("%d",b);
}
int shushuo(int a)
{
int half=a/2;
for(half;half>1;half--)
{
if(a%half==0)
{
return 0;
break;
}
}
return 1;
}