How to print prime numbers from 1 to n

0 votes

How to print prime number from 1 to n?

Oct 12, 2020 in Python by divyashree
• 120 points

recategorized Oct 13, 2020 by Niroj 72 views

1 answer to this question.

0 votes

Hello @divyashree

def isPrime(n):

    # Corner case

    if n <= 1 :

        return False

    # check from 2 to n-1

    for i in range(2, n):

        if n % i == 0:

            return False

    return True

  

# Function to print primes

def printPrime(n):

    for i in range(2, n + 1):

        if isPrime(i):

            print(i, end = " ")

  

# Driver code

if __name__ == "__main__" :

    n = 7

    # function calling

    printPrime(n)

Hope it helps!!

Thank you!!

      

answered Oct 13, 2020 by Niroj
• 82,580 points

Related Questions In Python

+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 4, 2018 in Python by Omkar
• 69,090 points

edited Aug 8, 2018 by Omkar 5,366 views
0 votes
1 answer

How to read numbers from file in Python?

Assuming you don't have extraneous whitespace: with open('file') ...READ MORE

answered Apr 16, 2019 in Python by SDeb
• 13,250 points
4,560 views
0 votes
0 answers

How to print square of first 100 natural numbers using iterations in python?

Can you make a program with nested ...READ MORE

Jul 22, 2019 in Python by Waseem
• 4,540 points
160 views
0 votes
2 answers
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,866 views
0 votes
0 answers
0 votes
1 answer

What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

Hii @kartik, As always with Android there's lots ...READ MORE

answered May 26, 2020 in Java by Niroj
• 82,580 points
1,007 views
+2 votes
12 answers
0 votes
1 answer

How to pretty-print a numpy.array without scientific notation and with given precision?

Hii @kartik, The numpy arrays have the method round(precision) which ...READ MORE

answered Apr 14, 2020 in Python by Niroj
• 82,580 points
981 views