Python

Python program to calculate sum of prime numbers between 1 to n

Python program to calculate sum of prime numbers between 1 to n

In this article, we will discuss the concept of Python program to calculate sum of prime numbers between 1 to n

In this code, we are going to learn how to find sum of prime numbers 1 to n using different methods in Python language.

This is done using for loop, in Python language

Code to display sum of prime numbers

Code to calculate sum of prime numbers using for loop

In this program, we will calculate sum of prime numbers 1 to n using for loop in Python language

Program 1

#takes input from the user
max=int(input("Enter the maximum value for find sum of primes:"))
sum=0
for num in range(2,max+1):
    i=2
    for i in range(2,num):
        if(int(num%i==0)):
            i=num
            break;
        #when the number is prime calculate sum
    if i is not num:
        sum+=num
print("Sum of all prime numbers 1 to ",max,":",sum)

When the above code is executed, it produces the following result

Enter the maximum value for find sum of primes:100
Sum of all prime numbers 1 to  100 :1058

 

Code to calculate sum of prime numbers between 1 to n -method 2

Program 2

#Python program to find sum of prime numbers from 1 to n
maximum=int(input("Please enter the maximum value: "))
total=0
for Number in range(1,maximum+1):
    count=0;
    for i in range(2,(Number//2+1)):
        if(Number%i==0):
          count=count+1
          break
    if(count==0 and Number !=1):
        
        total=total+Number
print("Sum of prime numbers from 1 to %d=%d"%(maximum,total))

When the above code is executed, it produces the following result

Please enter the maximum value: 50
Sum of prime numbers from 1 to 50=328

 

Code to calculate sum of prime numbers between 1 to n -method 3

Program 3

#Python program to find sum of prime numbers from 1 to n
maximum=int(input("Please enter the maximum value: "))
total=0
for Number in range(1,maximum+1):
    count=0;
    for i in range(2,(Number//2+1)):
        if(Number%i==0):
          count=count+1
          break
    if(count==0 and Number !=1):
       # print("%d"%Number)
        total=total+Number
print("Sum of prime numbers from 1 to %d=%d"%(maximum,total))

When the above code is executed, it produces the following result

Please enter the maximum value: 100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Sum of prime numbers from 1 to 100=1060

 

Suggested post

Python function

For loop in Python

while loop in Python

Operator in Python

Data types in Python

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.