Programming Examples

Find the sum of all the primes below a user-defined number.


Find the sum of all the primes below a user-defined number.

Solution

n = int(input("Enter a number: "))

sum_prime = 0

for num in range(2, n):
    is_prime = True

    for i in range(2, num):
        if num % i == 0:
            is_prime = False
            break

    if is_prime:
        sum_prime += num

print("Sum of all prime numbers below", n, "is:", sum_prime)
▶ RUN

Output/ Explanation:

Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad