#include<stdio.h>
int isodd(int n)
{
if(n%2)
return 1;
else
return 0;
}
void compare(int *x,int *y)
{
int temp;
if(*x>*y)
{
temp=*x;
*x=*y;
*y=temp;
}
}
void main()
{
int temp,sum=0,num1,num2;
printf("enter the num1 and num2:\n");
scanf("%d",&num1);
scanf("%d",&num2);
compare(&num1,&num2);
for(temp=num1;temp<=num2;temp++)
{if(isodd(temp))
sum=sum+temp;}
printf("the sum of the odd numbers between num1 and num2 is %d\n",sum);
}
#include <stdio.h>
void main()
{
int sum1,sum2,sum=0,i,n,leap;
printf("please input two numbers: \n");
scanf("%d%d",&sum1,&sum2);
if(sum1>=sum2)
{
printf("input error.\n");
exit(-1);
}
leap=sum1%2;
n=sum2-sum1-leap;
for(i=1;i<n;i+=2)
sum+=(sum1+i+leap);
printf("sum=%d",sum);
}
[此贴子已经被作者于2005-11-30 2:18:23编辑过]
#include<stdio.h>
main()
{
unsigned i,k,factor;
int a[101];
for(i=2;i<=100;i++) /*Initialization*/
a[i]=1;
factor=2;
while(factor<=100)
{
if(a[factor]==1)
{
printf("%2d\t",factor);
k=factor;
while(k<=100)
{
a[k]=-1;
k+=factor;
}
}
factor++;
}
putchar('\n');
}
/* Sieve Method: In ancient Greek, one scholar named Eratosthenes discover a method to find out prime numbers less than N. It is that place N natural numbers from minimum to maximum, the first number 'one' which is neither a prime number nor a divisible number need to be redlined, keep the second number 'two' which is a prime mumber, then redline all numbers which could be divided exactly by two, if n+1 is not be redlined before, make it a divisor, all numbers after it could be divided exactly by it should be redlined also. Do it so from 2 to N, numbers left are prime numbers. */