Ask your own question, for FREE!
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.

OpenStudy (anonymous):

writing code java

OpenStudy (kc_kennylau):

int count=0; for(int i=1; i<=100; i++){ count+=i^2; } System.out.println(count); //P.S. not tested

OpenStudy (anonymous):

are you sure?

OpenStudy (kc_kennylau):

test it for yourself?

OpenStudy (anonymous):

yes it's correct.thank you so much!

OpenStudy (kc_kennylau):

no problem :)

OpenStudy (anonymous):

oh! i have one more exercise.

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

OpenStudy (kc_kennylau):

Hint: for loop from i=1 to i<=10 :)

OpenStudy (kc_kennylau):

for(int i=0;i<=10;i++){ System.out.println("2×"+i+"="+2*i); }

OpenStudy (anonymous):

ok.i will test it.

OpenStudy (anonymous):

what about this: Write a program to sum only the odd number from 1 to 100.

OpenStudy (kc_kennylau):

Do I have to do everything for you xP

OpenStudy (kc_kennylau):

Hint: Odd numbers can be expressed in the form 2n+1

OpenStudy (anonymous):

yes

OpenStudy (kc_kennylau):

I won't do this this time, tell me your attempt and I try to correct it

OpenStudy (anonymous):

because this exercise i can't do it.i try again and again.but i not sure about this exercise!

OpenStudy (kc_kennylau):

Tell me what's in your mind :)

OpenStudy (anonymous):

about this exercise?

OpenStudy (kc_kennylau):

yes :)

OpenStudy (anonymous):

i think should be have a condition to notice odd number!

OpenStudy (kc_kennylau):

Hint: run i from 0 to 49, and make i odd by 2*i+1 :)

OpenStudy (anonymous):

how to sum it?

OpenStudy (kc_kennylau):

declare a variable called count outside the for loop :)

OpenStudy (anonymous):

i'm not clear about for loop.can you write it to me?

OpenStudy (kc_kennylau):

int count=_; for int(i=_;i<=__;i__){ count+=2*i+1; } Fill in the blanks :D

OpenStudy (anonymous):

i=0;i<=100;i++

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?

OpenStudy (anonymous):

For value of i will 2*i+1 produce 99.

OpenStudy (kc_kennylau):

huh?!

OpenStudy (kc_kennylau):

int count=0; for int(i=0;i<=49;i++){ count+=2*i+1; } System.out.println(count);

OpenStudy (anonymous):

all odd number from 1 to 100 not 1 to 50

OpenStudy (kc_kennylau):

2*i+1 gives you the odd numbers

OpenStudy (anonymous):

so it will be(i=1;i<=99;i++)

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}\]

OpenStudy (kc_kennylau):

I'm not adding i to the count each time.

OpenStudy (kc_kennylau):

I'm adding 2*i+1 to the count each time.

OpenStudy (anonymous):

yes this i understand!

OpenStudy (kc_kennylau):

:)

OpenStudy (kc_kennylau):

2i+1 is from 0 to 99

OpenStudy (kc_kennylau):

i mean 1 to 99

OpenStudy (kc_kennylau):

then i is from 0 to 49 :)

OpenStudy (anonymous):

i from 1 to 100

OpenStudy (kc_kennylau):

what?

OpenStudy (kc_kennylau):

I'm adding the odd integers from 1 to 100 together

OpenStudy (kc_kennylau):

But I don't use i to denote the integers

OpenStudy (anonymous):

yes i understand!

OpenStudy (kc_kennylau):

I use 2i+1 to let it run through the odd integers

OpenStudy (kc_kennylau):

Therefore i is from 0 to 49

OpenStudy (kc_kennylau):

So that 2i+1 will run through all the odd integers from 1 to 100

OpenStudy (kc_kennylau):

Then I add all 2i+1 to the count

OpenStudy (anonymous):

yes

OpenStudy (kc_kennylau):

Therefore i is from 0 to 49

OpenStudy (anonymous):

ok.i understand it all

OpenStudy (anonymous):

i will test it now

OpenStudy (kc_kennylau):

yay :)

OpenStudy (zzr0ck3r):

\[100(101)(201)/6\]

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!