2008年6月29日 星期日

20080602-Ho1

1. (50) Write a Java program to calculate the trianular function as follows:
Cos(x)=1 - 2!/x 2 + 4!/x4 - 6!/x6 ...

int Sum=1,x,i=1,a=1,b=1;//x為變數,第0項=1
for(;i<100;i++)//理論上i可以到無限大
{
for(int n=2i;n>1;n--)//分子
{a=a*n;}
for(;n>2i,n++)//分母
{b=b*x;}
if(i%2==0)
Sum=Sum+a/b;
if(i%2==1)
Sum=Sum-a/b;
}
----------------------------------------------------------------------
2. (30)Write a complete Java program that uses a for loop to compute the sum of the even numbers and the sum of the odd numbers between 1 and 50.

imt Sum=0,n=0;
while(n<25)
{Sum=Sum+2n+1;}
-----------------------------------------------------------------
3. (20) Rewrite the following for loop using a while expression.
int Sum=0;
for(i=0; i<=10; i++)
Sum=Sum+i;

int Sum=0,i=0;
while(i<=10)
{Sum=Sum+i;}

沒有留言: