How do you write a program that calculates the sum of odd numbers from 1-99?

I need to write a program using the for statement that calculates the sum of all odd numbers 1-99..

and if you do, can you please explain why you use the % symbol because i know you need to use that..

THANK YOU :D

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Using the mod symbol (%), you can find out if the number has a remainder. So if there's a remainder when dividing by 2, it's an odd number. So using the % operator, you can perform the following:

    int i;

    int sum=0;

    for(i=1; i<=99; i++) {

    if(i%2==1) // there's a remainder

    sum += i;

    }

    • Login to reply the answers
  • Eileen
    Lv 4
    4 years ago

    Formula should be: SUM= (N * (N+1))/2 (In this case, there are 100 numbers, so N=100) Answer is 5050 All you have to do then is set your variables, and create a message box.

    • Login to reply the answers
  • C B
    Lv 4
    1 decade ago

    int answer = 0;

    for(int i = 1; i <= 99; i = i + 2){

    answer += i;

    }

    that'll give you the answer, no mod (%) required. Feel free to message me with questions.

    Source(s): CS major
    • Login to reply the answers
Still have questions? Get your answers by asking now.