Question 2: Write a program that input a number from user, calculate the sum of prime
numbers from 1 to number entered by user.
-
-
Answers
Answered by
2
Answer:
Program to find sum of prime numbers between 1 to n
Write a program to find sum of all prime numbers between 1 to n.
Examples:
Input : 10
Output : 17
Explanation : Primes between 1 to 10 : 2, 3, 5, 7.
Input : 11
Output : 28
Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
A simple solution is to traverse all numbers from 1 to n. For every number, check if it is a prime. If yes, add it to result.
An efficient solution is to use Sieve of Eratosthenes to find all prime numbers from till n and then do their sum.
Similar questions