Find the sum of all numbers below 1000 which are multiples of 3 or 5 in Python

Here, we will learn how to find the sum of all numbers below 1000 which are multiples of 3 or 5 in the Python programming language? By Bipin Kumar Last updated : January 05, 2024

Problem statement

Sometimes, we need to find the sum of all integers or numbers that are completely divisible by 3 and 5 up to thousands, since thousands are a too large number that’s why it becomes difficult for us. So, here we will do it in Python programming language that solves the problem in just a few seconds. To solve this problem, we will use the range function. So, before going to find the sum we will learn a little bit about range function.

What is the range function in Python?

The range() function is a built-in function available in Python. In simple terms, the range allows them to generate a series of numbers within a given interval. This function only works with the integers i.e. whole numbers.

Syntax of range() function:

range(start, stop, step)

It takes three arguments to start, stop, and step and it depends on users choose how they want to generate a sequence of numbers? By default range() function takes steps of 1.

Python program to find the sum of all numbers below 1000 which are multiples of 3 or 5

# initialize the value of n
n=1000 
# initialize value of s is zero.
s=0 

# checking the number is divisible by 3 or 5
# and find their sum
for k in range(1,n+1):
    if k%3==0 or k%5==0: #checking condition 
        s+=k

# printing the result
print('The sum of the number:',s)

Output

The output of the above example is:

The sum of the number: 234168

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.