0% found this document useful (0 votes)
20 views

Python

The document contains code to check if a number is prime or not. It takes a number as input, then checks if it is divisible by any number between 2 and itself, excluding 1 and the number itself. If no factors are found, it prints that the number is prime, otherwise it prints that it is not prime along with one of the factors. It contains another example that takes the number 407 as input and correctly identifies it as not prime, printing the factors 11 and 37.

Uploaded by

Aayush Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python

The document contains code to check if a number is prime or not. It takes a number as input, then checks if it is divisible by any number between 2 and itself, excluding 1 and the number itself. If no factors are found, it prints that the number is prime, otherwise it prints that it is not prime along with one of the factors. It contains another example that takes the number 407 as input and correctly identifies it as not prime, printing the factors 11 and 37.

Uploaded by

Aayush Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

1 # Program to check if a number 1s prime or

not
2
3 num = 29
4
5 # To take input from the user
6 #num = int ( input ("Enter a number: "))
7
8 # define a flag variable
9 flag = False
10
11 • if num - - 1 :
12 print (num, "i s not a prime number" )
13 • eli f num > 1 :
14 # check for factors
15 • for i in range ( 2 , num ) :
16• if (num % i) == O:
17 # if factor is found, set flag
to True
18 flag = True
19 #breakout of loop
20 break
21
22 # check if flag is True
23 • if flag:
24 print (num, "is not a prime numbe r" )
25• else :
26 print (num, "i s a prime number" )
29 is a prime number
1 num = 407
2
3 # To take input from the user
4 #num - int ( input ("Enter a number: "))
5
6 • if num == 1 :
7 print (num, "is not a prime number
8• elif num > 1:
9 # check for factors
10• for i in range( 2 ,num):
11 • if ( num % i) == O:
12 print (num, "is not a prime
number" )
13 print (i, "times" ,num // i, "is
14 break
15• else :
16 print (num, "is a prime nun,ber" )
17
18 # if input number 1S less than
19 # or equal to 1 , it 1S not prime
20 • else :
---
21 print (num, "is not a prime nurnber "[)
407 is not a prime number
11 times 37 is 407
1 number = o
2
3• if number > O:
4 pr in t ( "P os iti ve nu mb er" )
5
6• el if number == O:
7 pr in t ( 'Z er o' )
8 • el se :
9 pr in t ( 'N eg ati ve number' )
10

11 pr in t ( 'T his std tem en t is always ex ec ut


Zero
1 number = 5
2
3 #outer if statement
4 • if (number > = O) :
5 # inner if statement
6• if number == O:
7 print ( 'Number is O' )
8
9 # inner else statement
10• else :
11 print ( 'Number is positive' )
12
13 #outer else statement
14 • else :
15 print ( 'Number is negative' )
Number is positive
1 # program to fin d fir st 5 mu lti pl es of
2
3 i = 1
4
5 • wh i l e ( i < = 1O) :
6 pr int ( '6 * ' , ( i) , '=' ,6 * i)
7
8• i f i >= 5:
9 break
10
11 i = i + 1
6 * 1 -- 6
6 * 2 - 12
6 * 3 - 18
6 * 4 - 24
6 * 5 -- 30
# Python program to display all the prime
numbers within an interval

lower - 900
upper - 1000

print ( "Prime numbers betwee n" , lower, "and"


, upper, "are : " )

for num in range(lower, upper + 1):


# all prime numbers are greater than 1
i f num > 1:
f or i i n range(2, num):
i f (num % i) == 0:
break
el se :
pr i n~(}1um)
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997
1 # prog ra m to print odd numbers fro m 1
2
3 num = O
4
S• while num < 10 :
6 num += 1
7
8• if (num % 2) 0:
9 continue
10
11 print (hum)
1
3
5
7
9
1 # Program to separate +ve and -ve
2 X = [ 23 , 4 , - 6 , 23 , - 9 , 2 1 , 3 , - 45 , - 8 ]
3 pos - []
4 neg=[]
5 ... for i in range ( len (x)):
6 ... if x[i] < O:
7 neg. append (x[i])
8 ... else :
9 pos. append (x[i])
10 print ( Positive numbers are: ,pos)
11 II

1 1 print ( Negative numbers are:


11 II
,neg)
Positive numbers are: [23, 4, 23, 21,
3]
Negative numbers are: [ -6, -9, -45, -8~
1 # WAP to filter even and odd number form a
list
2 X = [ 10 , 23 , 24 , 35 , 65 , 78 , 90 ]
3 eve - []
4 odd=[]
5 ... for 1 1n range ( len (x)):
6 ... if X [ i] % 2 == 0 :
7 eve. append (x[i])
8 ... else :
9 odd. append (x[i])
10 print ( "Even numbers are: " ,eve)
11 p r in ti("Odd numbers a re : " , odd') I
Even numbers are: [10, 24, 78, 90]
Odd numbers are: [23, 35, 65]
1 # Factorial of a number
2 n = int ( input ( "Enter number: " )) # 5
3 ... if ( n == O or n < O) :
4 print ( "Value of n should be greater
than 1" )
5 ... else :
6 fact= 1
7 ... while ( n) :
8 fact*= n
9 n = n- 1
10 print ( f"Factorial is {fact} " )
Factorial is 120

You might also like