eg1: 求1-100 的和
#include
int main(int argc,char *argv[])
{
int sum = 0;
int i = 1;
/*
while(i <= 100)
{
sum += i;
i++;
}
*/
do
{
sum +=i++;
}while(i < 101);
printf("sum = %d\n",sum);
return 0;
}
eg2:从键盘输入一个数字,判断他是几位数
#include
int main(int argc,char *argv[])
{
int num;
int count = 0;
scanf("%d",&num);
if(num == 0)
count = 1;
while(num != 0)
{
num = num / 10;
count++;
}
printf("count = %d\n",count);
return 0;
}
eg3:从键盘输入字符,判断在\n之前有多少个字符
#include
int main(int argc,char *argv[])
{
char ch;
int num = 0;
/*
do
{
ch = getchar();
num++;
}while(ch != '\n');
printf("num = %d\n",num-1);
*/
while((ch =getchar() )!= '\n')
{
num++;
}
printf("num = %d\n",num);
return 0;
}
eg4:打印9 * 9 乘法表
#include
int main(int argc,char *argv[])
{
int i,j;
for(i = 1;i <= 9;i++)
{
for(j = 1;j <= i;j++)
{
printf("%d * %d = %d\t",j,i,i*j);
}
printf("\n");
}
return 0;
}
eg6: 从键盘输入一个数字,判断这个数是不是素数,如果是,输出YES,否则输出NO
3) 辗转相除法(欧几里得算法)
这里举一个列子
#include
int main(int argc,char *argv[])
{
int a,b;
int max;
scanf("%d%d",&a,&b);
int i,j;
if(a > b)
i = b;
else
i = a;
/*
for(j = 1;j <= i;j++)
{
if(a % j == 0 && b % j == 0)
{
max = j;
}
}
*/
for(j = a; j>= 1;j--)
{
if(a % j == 0 && b % j == 0)
{
max = j;
break;
}
}
printf("max = %d,min = %d\n",max,a*b/max);
return 0;
}