Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Java program to find sum of prime numbers from 1 to n

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

Posted on December 10, 2020

Table of Contents

  • Python program to calculate sum of prime numbers between 1 to n
    • Code to display sum of prime numbers
      • Code to calculate sum of prime numbers using for loop
      • Code to calculate sum of prime numbers between 1 to n -method 2
      • Code to calculate sum of prime numbers between 1 to n -method 3

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes