星期五, 11月 09, 2007

Partial Solutions to Quiz II

Partial solutions: The following solutions are not complete Java programs. They are kept brief to illustrate the main ideas of the complete programs.

Problem 1:

double power=1, fact=1, sum=0;
int k=-1;

for(int i=1; i<10; i++)
{
power=power*x;
fact=fact*i;
if(i%2==1)
{ k=k*(-1);
sum+=k*power/fact;
}
}


Problem 2:

int evenSum=oddSum=0;

for(i=1; i<=25; i++)
{
if(i%2==0)
evenSum=evenSum+i;
else
oddSum+=i;

}

沒有留言: