English

Write a program to find all prime numbers up to given number.

Advertisements
Advertisements

Question

Write a program to find all prime numbers up to given number.

Code Writing
Advertisements

Solution

A prime number is divisible only by 1 and itself; test divisibility for each number from 2 to n.

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

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

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

    if is_prime:
        print(num, end=" ")
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Review of Phython - EXERCISE [Page 27]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 13. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×