Assignment # 1: 1.write A Program in Python To Print PRIME Numbers Between 1 To N, Where The
Assignment # 1: 1.write A Program in Python To Print PRIME Numbers Between 1 To N, Where The
#1
Code:
n = int(input("Enter the value of n: "))
print("Prime numbers are: ")
l=[]
m=[]
i=1
while i<=n:
j=2
while j<i:
if i%j==0:
break
j=j+1
else:
if j not in l:
l.append(j)
i=i+1
for i in l:
print(i)
OUTPUT:
Enter the value of n: 10
Prime numbers are:
2
3
5
7
>>>
Code:
num = int(input("Enter a number: "))
sum = 0
t = num
while t > 0:
digit = t%10
sum += digit**3
t//=10
if num == sum:
print(num,"is an armstrong number.")
else:
print(num,"is not an armstrong number.")
OUTPUT:
Enter a number: 153
153 is an armstrong number.
Enter a number: 343
343 is not an armstrong number.
>>>
3.Write a program to print all the ARMSTRONG numbers between 0 to N, where the
value of N is to be entered by the user.
Code:
n=int(input("Enter a number: "))
for i in range(n+1):
s=0
t=i
while t > 0:
digit = t % 10
s += (digit ** 3)
t //= 10
if i==s:
print(i)
OUTPUT:
Enter a number: 500
0
1
153
370
371
407
>>>
4.Write a program in Python to find the HCF of two given numbers (integers).
Enter N:12 Enter N: 24
Enter M:18 Enter M: 16
HCF :6 HCF : 8
More(Y/N)? Y More(Y/N)? N
Code:
more = "Y"
while more=="Y":
m=int(input("Enter n: "))
n=int(input("Enter m: "))
l=[]
for i in range(2,n+1):
if m%i==0 and n%i==0:
l.append(i)
print("HCF:",l[-1])
more = input("More(Y/N): ")
if more=="Y":
continue
else:
break
OUTPUT:
Enter n: 12
Enter m: 18
HCF: 6
More(Y/N): Y
Enter n: 24
Enter m: 16
HCF: 8
More(Y/N): N
>>>
5.Write a program in Python to find the LCM of two given numbers (integers).
Enter N:12 Enter N: 24
Enter M:18 Enter M: 16
LCM :36 LCM : 48
Code:
more = "Y"
while more=="Y":
m=int(input("Enter n: "))
n=int(input("Enter m: "))
l=[]
for i in range(1,(m*n)+1):
if i%m==0 and i%n==0:
l.append(i)
print("LCM:",min(l))
more = input("More(Y/N): ")
if more=="Y":
continue
else:
break
OUTPUT:
Enter n: 12
Enter m: 18
LCM: 36
More(Y/N): Y
Enter n: 24
Enter m: 16
LCM: 48
More(Y/N): N
>>>
6.Write a program to print all the perfect numbers from 1 to N. A perfect number
is one whose sum of factors (excluding the number itself) is equal to the number.
Example: 6 is a perfect number (6 = 1 + 2 + 3)
If N = 100, the program must print 6 and 28
Code:
n=int(input("Enter an integer: "))
for x in range(1,n+1):
l=[]
for i in range(1,x):
if x%i==0:
l.append(i)
if sum(l)==x:
print(x,"is a perfect number")
OUTPUT:
Enter an integer: 100
6 is a perfect number
28 is a perfect number
>>>
7.Write a program to find and display the sum of the following series for n terms
(using loops):
a.X+ X2/2! + X3/3!+ ... N Terms
Code:
x = int(input("Enter the value of x: "))
n = int(input("Enter the number of terms: "))
sum = 0
m=1
for i in range(1, n) :
fact = 1
for j in range(1, i+1) :
fact *= j
term = x ** i / fact
sum += term * m
print("Sum =", sum)
OUTPUT:
Enter the value of x: 10
Enter the number of terms: 20
Sum = 21949.37884943194
>>>
b.X-X2/2!+ X3/3! - ... N Terms
Code:
x = int(input("Enter the value of x: "))
n = int(input("Enter the number of terms: "))
sum = 0
m=1
for i in range(1, n) :
fact = 1
for j in range(1, i+1) :
fact *= j
term = x ** i / fact
sum += term * m
m = m * -1
print("Sum =", sum)
OUTPUT:
Enter the value of x: 10
Enter the number of terms: 20
Sum = 28.70631023742594
>>>
OUTPUT:
Enter limit: 20
The sum of the series is 1540
>>>
Code:
n = int(input("Enter the number of terms: "))
sum = 0
for i in range(0, n + 1):
k=2
for j in range(1, i + 1):
sum = sum + k;
k=k+2
print("The sum of the series is", sum)
OUTPUT:
Enter the number of terms: 20
The sum of the series is 3080
>>>
8.Write a program to input a number N and print the fibonacci series till N.
Example: if N = 7, the program should print
0,1,1,2,3,5,8
Code:
num = int(input("Enter the Number:"))
n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ")
for i in range(2, num):
n3 = n1 + n2
n1 = n2
n2 = n3
print(n3, end=" ")
print()
OUTPUT:
Enter the Number:7
Fibonacci Series: 0 1 1 2 3 5 8
>>>
9.Write a program to input a number N and print all the terms of the fibonacci
series which are less than or equal to N.
Example: if N = 20, the program should print
0,1,1,2,3,5,8,13
Code:
num = int(input("Enter the Number:"))
n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ")
for i in range(2, num):
n3 = n1 + n2
n1 = n2
n2 = n3
if(n3<num):
print(n3, end=" ")
print()
OUTPUT:
Enter the Number:20
Fibonacci Series: 0 1 1 2 3 5 8 13
>>>