Knowee
Questions
Features
Study Tools

Write a python script that accept a number from the user and display a prime number from 1 to the value:

Question

Write a python script that accept a number from the user and display a prime number from 1 to the value:

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Python script that accepts a number from the user and displays all prime numbers from 1 to that value:

def is_prime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    i = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i += 2
    return True

num = int(input("Enter a number: "))
for i in range(1, num + 1):
    if is_prime(i):
        print(i)

Here's how it works:

  1. The is_prime(n) function checks if a number n is prime. It returns True if n is prime and False otherwise.
  2. The script then asks the user to input a number and stores it in the variable num.
  3. The script then loops from 1 to num (inclusive) and for each number i in this range, it checks if i is prime using the is_prime(i) function.
  4. If i is prime, it is printed to the console.

This problem has been solved

Similar Questions

# Write a function in python which accept a number from user to return True, if the number#is a prime number else return False. Use this function to print all prime numbers from 1#to 100.

prime number in python

Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime.

Write a single line Python code to print all the prime numbers between 1 and 200.

Create a Python function that takes a list of integers as input andreturns a new list containing only the prime numbers from the input list.Provide a clear explanation of your implementation and include a test case.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.