Monday, October 15, 2018

Python program to print Prime numbers

Python program to print Prime numbers

  • Python variables do not need explicit declaration to reserve memory space.
  • The declaration happens automatically when you assign a value to a variable.
  • The equal sign (=) is used to assign values to variables.
  • Statements always end in a NEWLINE.
  • A block is a group of statements in a program. code blocks are defined by their indentation in Python.

Sample Python Program with Output is given below
Print Prime numbers between 1 and 100


i=2
while i<=100:
    prime=True
    j=2
    while j<i and prime==True:
        if i % j ==0:
            prime=False
        j=j+1
    if prime==True:
        print(i)
    i=i+1

PythonSource
PythomOutputPrimeNumbers


Note Efficient Python program to print Prime numbers

i=2
while i<=100:
    prime=True
    j=2
    while j<=int(i/2) and prime==True:
        if i % j ==0:
            prime=False
        j=j+1
    if prime==True:
        print(i)
    i=i+1

  


No comments:

Post a Comment