Subido por donka doom

HW3

Anuncio
1. a) for (int i = 0; i < Math.sqrt(n); i++) {
out.println((int) Math.pow(i, 2));
}
b) for (int i = 1; i < n; i++) {
if (10 * i < n) {
out.println(10 * i);
} else {
break;
}
}
c) for (int i = 0; i < n; i++) {
if ((int) Math.pow(2, i) > n) {
break;
} else {
out.println((int) Math.pow(2, i));
}
}
2. a)
Statement Variable
i=0, j=10, n=0
While (i<j)
i++
i= 1 2 3 4 5
j-j=9 8 7 6 5
n++
i=5, j=5, n=5
b)
Statement Variable
i=0, j=10, n=0
While(i<10)
i++
i=1 2 3 4 5 6 7 8 9 10
n=n+i+j
n=1 4 9 16 25 36 49 64 81 100
j++ j= 1 2 3 4 5 6 7 8 9 10
i=10, j=100, n=10
c)
Statement Variable
i=0, j=10, n=0
while (i>0)
i-i=9 8 7 6 5 4 3 2 1 0
j++
j=1 2 3 4 5 6 7 8 9 10
n=n+i-j
n=8 14 18 20 20 18 14 8 0 -10
i=0, j=10, n=-10
d)
Statement Variable
i=0, j=10, n=0
While (i != j)
I = i+2
i = 2 4 6 8 10 12 14 -> infinity
J = j-2
j=8 6 4 2 0 -2 -4 -> -infinity
n++
n=1 2 3 4 5 6 7 8 -> infinity
i= infinity -> error
j=-infinity -> error
n=-infinity -> error
e)
Statement Variable
i=3, j=4, n=0
while (i !=0)
n+=j
i=2 1 0
i-j=4 4 4
n=4 8 12
i=0, j=4, n=12
3.
int s = 0;
Int i = 1;
while(i<=10){
s=s+i;
i++;
}
4)
int i=0;
while(n>0){
pi = pi + ((Math.pow(-1, i))/((2*i)+1));
I++;
N--;
}
pi=pi*4;
out.println(pi);
5)
int n=1;
int m=1;
while (Math.pow(n, 2) < areaBound){
while(Math.pow(m, 2) < areaBound){
sum=sum+ Math.pow(m, 2) < areaBound+Math.pow(n, 2) < areaBound;
m++;
}
m=1;
n++;
}
out.println(sum);
Descargar