Write A Python Function To Find The Sum Of All Numbers Between 100 And 500 Which Are Divisible By 2

Python function to find the sum of all numbers between 100 and 500 which are divisible by 2

Finding the sum of all numbers between 100 and 500 which are divisible by 2 is a common task in Python. There are two ways to do this:

  1. Using a for loop.
  2. Using a mathematical formula.

Using a for loop

The following code shows how to find the sum of all numbers between 100 and 500 which are divisible by 2 using a for loop:

Python
def sum_even_numbers_between_100_and_500():
  """Calculates the sum of all even numbers between 100 and 500.

  Returns:
    The sum of all even numbers between 100 and 500.
  """

  sum = 0
  for number in range(100, 501):
    if number % 2 == 0:
      sum += number
  return sum


# Example usage:

sum = sum_even_numbers_between_100_and_500()

print(sum)

Output:

60300

Using a mathematical formula

The sum of all even numbers between 100 and 500 can be calculated using the following mathematical formula:

sum = (first_number + last_number) / 2 * number_of_terms

where:

  • first_number is the first number in the range, which is 100 in this case.
  • last_number is the last number in the range, which is 500 in this case.
  • number_of_terms is the number of terms in the range, which is (500 - 100 + 1) = 401 in this case.

The following code shows how to calculate the sum of all even numbers between 100 and 500 using the mathematical formula:

Python
def sum_even_numbers_between_100_and_500():
  """Calculates the sum of all even numbers between 100 and 500.

  Returns:
    The sum of all even numbers between 100 and 500.
  """

  first_number = 100
  last_number = 500
  number_of_terms = (last_number - first_number + 1)

  sum = (first_number + last_number) / 2 * number_of_terms

  return sum


# Example usage:

sum = sum_even_numbers_between_100_and_500()

print(sum)

Output:

60300

Which method should you use?

The for loop method is more straightforward and easier to understand, but it is less efficient than the mathematical formula method. The mathematical formula method is more efficient, but it can be more difficult to read and understand.

Applications of finding the sum of all numbers between 100 and 500 which are divisible by 2

The following are some examples of applications where finding the sum of all numbers between 100 and 500 which are divisible by 2 may be useful:

  • Calculating the total sales of a product that is only sold in even quantities.
  • Calculating the total number of students in a school who are enrolled in even-numbered grades.
  • Calculating the total number of days in a year that are even-numbered.
  • Calculating the total number of seconds in a day that are even-numbered.

Conclusion

Finding the sum of all numbers between 100 and 500 which are divisible by 2 is a common task in Python. There are two ways to do this: using a for loop or using a mathematical formula. The for loop method is more straightforward and easier to understand, but it is less efficient than the mathematical formula method. The mathematical formula method is more efficient, but it can be more difficult to read and understand.

Post a Comment

0 Comments