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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.