How to print prime numbers from 1 to n?

0 votes

How to print prime number from 1 to n?

Oct 12 in Python by divyashree
• 120 points

recategorized Oct 13 by Niroj 43 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 by Niroj
• 78,080 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,030 points

edited Aug 8, 2018 by Omkar 4,760 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
3,089 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
134 views
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,562 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 in Java by Niroj
• 78,080 points
605 views
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 in Python by Niroj
• 78,080 points
663 views
0 votes
1 answer

How to remove a key from a Python dictionary?

Hello, If you need to remove a lot ...READ MORE

answered Apr 15 in Python by Niroj
• 78,080 points
92 views