Mathematics
21 Online
OpenStudy (anonymous):
Write a program to find the “sum of the squares” of the numbers from 1 to 100 ,e,g,1*1 + 2*2 + 3*3 +…+ 100*100.
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (anonymous):
writing code java
12 years ago
OpenStudy (kc_kennylau):
int count=0;
for(int i=1; i<=100; i++){
count+=i^2;
}
System.out.println(count);
//P.S. not tested
12 years ago
OpenStudy (anonymous):
are you sure?
12 years ago
OpenStudy (kc_kennylau):
test it for yourself?
12 years ago
OpenStudy (anonymous):
yes it's correct.thank you so much!
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
no problem :)
12 years ago
OpenStudy (anonymous):
oh! i have one more exercise.
12 years ago
OpenStudy (anonymous):
Write a program to display multiplication table from two to nine for example:
2 x 1 = 2 …….. 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
12 years ago
OpenStudy (kc_kennylau):
Hint: for loop from i=1 to i<=10 :)
12 years ago
OpenStudy (kc_kennylau):
for(int i=0;i<=10;i++){
System.out.println("2×"+i+"="+2*i);
}
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (anonymous):
ok.i will test it.
12 years ago
OpenStudy (anonymous):
what about this:
Write a program to sum only the odd number from 1 to 100.
12 years ago
OpenStudy (kc_kennylau):
Do I have to do everything for you xP
12 years ago
OpenStudy (kc_kennylau):
Hint: Odd numbers can be expressed in the form 2n+1
12 years ago
OpenStudy (anonymous):
yes
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
I won't do this this time, tell me your attempt and I try to correct it
12 years ago
OpenStudy (anonymous):
because this exercise i can't do it.i try again and again.but i not sure about this exercise!
12 years ago
OpenStudy (kc_kennylau):
Tell me what's in your mind :)
12 years ago
OpenStudy (anonymous):
about this exercise?
12 years ago
OpenStudy (kc_kennylau):
yes :)
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (anonymous):
i think should be have a condition to notice odd number!
12 years ago
OpenStudy (kc_kennylau):
Hint: run i from 0 to 49, and make i odd by 2*i+1 :)
12 years ago
OpenStudy (anonymous):
how to sum it?
12 years ago
OpenStudy (kc_kennylau):
declare a variable called count outside the for loop :)
12 years ago
OpenStudy (anonymous):
i'm not clear about for loop.can you write it to me?
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
int count=_;
for int(i=_;i<=__;i__){
count+=2*i+1;
}
Fill in the blanks :D
12 years ago
OpenStudy (anonymous):
i=0;i<=100;i++
12 years ago
OpenStudy (kc_kennylau):
no, you wanna add the odd numbers from 1 to 99
For what value of i will 2*i+1 produce 1?
For what value of i will 2*i+1 produce 99?
12 years ago
OpenStudy (anonymous):
For value of i will 2*i+1 produce 99.
12 years ago
OpenStudy (kc_kennylau):
huh?!
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
int count=0;
for int(i=0;i<=49;i++){
count+=2*i+1;
}
System.out.println(count);
12 years ago
OpenStudy (anonymous):
all odd number from 1 to 100 not 1 to 50
12 years ago
OpenStudy (kc_kennylau):
2*i+1 gives you the odd numbers
12 years ago
OpenStudy (anonymous):
so it will be(i=1;i<=99;i++)
12 years ago
OpenStudy (kc_kennylau):
\[\begin{array}{c|c}
i&0&1&2&3&4&5&6&7&8&9&10\\\hline
2i+1&1&3&5&7&9&11&13&15&17&19&21
\end{array}\]
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
I'm not adding i to the count each time.
12 years ago
OpenStudy (kc_kennylau):
I'm adding 2*i+1 to the count each time.
12 years ago
OpenStudy (anonymous):
yes this i understand!
12 years ago
OpenStudy (kc_kennylau):
:)
12 years ago
OpenStudy (kc_kennylau):
2i+1 is from 0 to 99
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
i mean 1 to 99
12 years ago
OpenStudy (kc_kennylau):
then i is from 0 to 49 :)
12 years ago
OpenStudy (anonymous):
i from 1 to 100
12 years ago
OpenStudy (kc_kennylau):
what?
12 years ago
OpenStudy (kc_kennylau):
I'm adding the odd integers from 1 to 100 together
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
But I don't use i to denote the integers
12 years ago
OpenStudy (anonymous):
yes i understand!
12 years ago
OpenStudy (kc_kennylau):
I use 2i+1 to let it run through the odd integers
12 years ago
OpenStudy (kc_kennylau):
Therefore i is from 0 to 49
12 years ago
OpenStudy (kc_kennylau):
So that 2i+1 will run through all the odd integers from 1 to 100
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
Then I add all 2i+1 to the count
12 years ago
OpenStudy (anonymous):
yes
12 years ago
OpenStudy (kc_kennylau):
Therefore i is from 0 to 49
12 years ago
OpenStudy (anonymous):
ok.i understand it all
12 years ago
OpenStudy (anonymous):
i will test it now
12 years ago
Join the QuestionCove community and study together with friends!
Sign Up
OpenStudy (kc_kennylau):
yay :)
12 years ago
OpenStudy (zzr0ck3r):
\[100(101)(201)/6\]
12 years ago