You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a=int(input("Enter the start of the range : "))
b=int(input("Enter the end of the range : "))
defis_prime(num):
ifnum<2 : returnTrueforiinrange(2, (num//2)+1):
ifnum/i==num//i:
returnTruereturnFalsefornuminrange(a, b+1):
ifis_prime(num):
print(num)
2. program to print the first N fibonacci series.
n=int(input("Enter the number of terms : "))
a=0;
b=1;
foriinrange(0, n):
print(a)
c=a+ba=bb=c
3. program to find the roots of a quadratic equation(rounded to 2 decimal places).
importmathprint("Enter the values of a, b, c in (ax^2 + bx + c) : ")
a=int(input("Enter the value of a : "))
b=int(input("Enter the value of b : "))
c=int(input("Enter the value of c : "))
d= (b**2-4*a*c)
ifd>0:
root1= (-(b) +math.sqrt(d)) / (2*a)
root2= (-(b) -math.sqrt(d)) / (2*a)
print(f"Roots are real and different\nRoot 1 : {root1:.2f}\nRoot 2 : {root2:.2f}")
elifd<0:
real=b/2*aimag=math.sqrt(-1*d) / (2*a)
print(f"Roots are complex and different\nRoot 1 : {real:.2f} + {imag:.2f}i\nRoot 2 : {real:.2f} - {imag:.2f}i")
else:
root=-b/ (2*a)
print(f"Roots are real and same\nRoot : {root:.2f}")
4. program to check weather a given number is perfect or not (sum of factors = num).
num=int(input("Enter a number : "))
defis_perfect(num):
sum=0foriinrange(1, num):
ifnum//i==num/i:
sum+=ireturnsum==numifis_perfect(num):
print(f"Number {num} is perfect")
else:
print(f"Number {num} is not perfect")
5. program to display armstrong number up-to 1000.
6. program to perform bubble sort on a given set of elements.
n=int(input("Enter the number of terms : "))
a= []
foriinrange(0, n):
a.append(int(input(f"Enter number {i+1} : ")))
print("List before sorting : ", a)
foriinrange(0, n-1):
forjinrange(0, n-i-1):
ifa[j] >a[j+1]:
temp=a[j+1]
a[j+1] =a[j]
a[j] =tempprint("Bubble sorted list is : ", a)
7. program to accept a 10 digit mobile number and find the digits which are absent in it.
num=int(input("Enter a mobile number : "))
numbers= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
iflen(str(num)) ==10:
whilenum>0:
digit=num%10ifdigitinnumbers:
numbers.remove(digit)
num//=10print(numbers)
else:
print("Mobile number should contain 10 numbers.")
Cycle 2
1. Create a three dimensional array specifying float data type and print it.
4. create one dimensional array using arange function containing 10 elements and display
a. first 4 elements
b. last 6 elements
c. elements from index 2 to 7
importnumpyasnparr=np.arange(10)
first_4=arr[:4]
last_6=arr[-6:]
ele_2_to_7=arr[2:8]
print("original array : ", arr)
print("first 4 elements : ", first_4)
print("last 6 elements : ", last_6)
print("elements from index 2 to 7 : ", ele_2_to_7)
5. create a 1D array with arange contaning first 15 even numbers as elements
a. elements from indexing 2 to 8 with step 2
b. last 3 elements of the array using negative index
c. alternate elements of the array
d. display last 3 alternate elements
importnumpyasnparr=np.arange(2, 31, 2)
slice_arr=arr[2:9:2]
last_3=arr[-3:]
alternate_ele=arr[::2]
last_3_alternate=arr[-3*2::2]
print("original array : ", arr)
print("sliced array : ", slice_arr)
print("last 3 elements in array : ", last_3)
print("alternate elements : ", alternate_ele)
print("last 3 alternate elements : ", last_3_alternate)
6.
importnumpyasnparr=np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[10, 11, 12, 13],
[14, 15, 16, 17]
])
print("original array : ", arr)
print("elements excluding 1st row : ", arr[1:])
print("elements excluding last col : ", arr[:, :-1])
print("elements of first and second column in the 2nd and 3rd row : ", arr[1:3, 0:2])
print("elements of 2nd and 3rd column : ", arr[:, 1:3])
print("2nd and 3rd elements of the 1st row : ", arr[0, 1:3])
print("elements from indices 4 to 10 in desc order : ", arr[0])