-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssignment 12.py
More file actions
152 lines (103 loc) · 3.29 KB
/
Assignment 12.py
File metadata and controls
152 lines (103 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Assignment 12
# More on loops
# 1. Write a python script to reverse a number.
n=int(input("Enter a number:\t"))
rev=0
while n:
rem=n%10
rev=rev*10+rem
n//=10
print('The number in reverse is:',rev)
# or
num= input('Enter a number:')
rev_num=num[::-1]
print('The number in reverse is:',int(rev_num))
# 2. Write a python script to check whether a given number is Prime or not
n=int(input("Enter a number:\t"))
for i in range(2,(n//2)+1):
if n%i==0:
print('The nmber is not prime')
break
else:
print('The number is prime')
# 3. Write a python script to print all Prime numbers under 100
for r in range(1,101):
for i in range(2,(r//2)+1):
if r%i==0:
break
else:
print(r,end=' ')
# 4. Write a python script to print all Prime numbers between two given numbers (both values inclusive)
s=int(input("Enter starting range:\t"))
e=int(input('Enter ending range:\t'))
for i in range(s,e+1):
for j in range(2,(i//2)+1):
if i%j==0:
break
else:
print(i,end=' ')
# 5. Write a python script to find next prime number of a given number
n=int(input('Enter a number:\t'))
while 1:
n+=1
for i in range(2,(n//2)+1):
if n%i==0:
break
else:
print('The next prime number is:',n)
break
# 6. Write a python script to print first N prime numbers
n=int(input('Enter a number:\t'))
flag=1
while n:
while 1:
for i in range(2,(flag//2)+1):
if flag%i==0:
break
else:
print(flag,end=' ')
n-=1
flag+=1
break
flag+=1
# 7. Write a python script to check whether a given pair of numbers are co-Prime numbers or not.
n1,n2=int(input('enter a pair of numbers:')), int(input())
if n1>n2:s=n2
else:s=n1
for i in range(2,s+1):
if n1%i==0 and n2%i==0:
print('The numbers are not co-prime')
break
else:
print('The numbers are co-prime')
# 8. Write a python script to print first N terms of a Fibonacci series
n=int(input('How many numbers you want to print:\t'))
if n<=0:
print('invalid input!.....')
quit()
else:
f,s=0,1
for i in range(n):
print(f,end=' ')
f,s=s,f+s
# 9. Write a python script to calculate LCM of two numbers
n1,n2=int(input('Enter two number:\n')),int(input())
if n1<n2:
lcm=n1
else:
lcm=n2
while True:
if lcm%n1==0 and lcm%n2==0:
print('The lcm is',lcm)
break
lcm+=1
# 10. Write a python script to calculate HCF of two numbers
n1,n2=int(input('Enter two numbers:')),int(input())
if n1>n2:
s=n2
else:
s=n1
for i in range(s,0,-1):
if n1%i==0 and n2%i==0:
print('The hcf is:',i)
break