Home » Python » Python programs

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?
Submitted by Bipin Kumar, on December 06, 2019

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() 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.

Program:

# 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 sum of the number: 234168






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.








Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.