Practical 3 Aiml: Done by Anish: T20012531007

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

PRACTICAL 3

AIML

DONE BY :
ANISH
T20012531007
1) Python Program to Calculate the Average of
Numbers in a given list.
Code:
n=int(input("Enter the number of elements to
be inserted: "))
a=[]

for i in range(0, n):


element=int(input("Enter element: "))
a.append(element)
avg=sum(a)/n n=int(input("Enter the number
of elements to
print("Average of elements in list",
round(avg,2))

Result:
2)Python Program to Exchange the Values of
Two Numbers Without Using a Temporary
Variable
Code:
a=int(input("Enter the value of first
variable a:"))
b=int(input("Enter the value of second
variable b:"))
a=a+b
b=a-b
a=a-b
print("a is: ",a,"b is: ",b)

Result:

3) Python Program to Read a Number n and Compute


n+nn+nnn
Code:
n=int(input("Enter a number n:"))
temp=str(n)
t1=temp+temp
t2=temp+temp+temp
comp=n+int(t1)+int(t2)
print("The value is: ",comp)

Result:

4) Python Program to Reverse a Given Number


Code:
n=int(input("Enter the number you want to
reverse:"))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number is :",rev)

Result:

5) Python Program to Check Whether a Number is Positive


or Negative

Code:
n=int(input("Enter a number:"))
if n>0:
print("The number is positive")
else:
print("The number is negative")

Result:

6) Python Program to Take in the Marks of 5 Subjects and


Display the Grade
Code:
x=input("Enter the marks of your 5 subjects
separated by a space:")

x=x.split(" ")
x= list(map(int, x))
y=len(x)
z=sum(x)/y
if 100>z>90:
print("Your grade is A")
elif 90>z>75:
print("Your grade is B")
elif 75>z>60:
print("Your grade is C")
else:
print("You need to work harder")

Result:
7) Python Program to Print all Numbers in a Range
Divisible by a Given Number
Code:
a=int(input("Enter the lower limit:"))
b=int(input("Enter the upper limit:"))
c=int(input("Enter the number you want to
divide with:"))

for i in range(a+1,b+1):
if i%c==0:
print(i)

Result:

8) Python Program to Read Two Numbers and Print Their


Quotient and Remainder
Code:
x=int(input("Enter the first number:"))
y=int(input("Enter the second number:"))

print("Quotient:",x/y)
print("Remainder:",x%y)

Result:

9) Python Program to Accept Three Digits and Print all


Possible Combinations from the Digits
Code:
x=int(input("Enter first number:"))
y=int(input("Enter second number:"))
z=int(input("Enter third number:"))
d=[]
d.append(x)
d.append(y)
d.append(z)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(d[i],d[j],d[k])
Result:

10) Python Program to Print Odd Numbers Within a


Given Range
Code:
x=int(input("Enter the lower limit:"))
y=int(input("Enter the upper limit:"))

for i in range(x+1,y+1):
if i%2!=0:
print(i)
Result:

11) Python Program to Find the Sum of Digits in a


Number
Code:
x=int(input("Enter any with more than one
digit number:"))
res = [int(x) for x in str(x)]
z=sum(res)
print(z)

Result:

12) Python Program to Find the Smallest Divisor of an


Integer
Code:
x=int(input("Enter an integer:"))
y=[]
for i in range(2,x+1):
if(x%i==0):
y.append(i)
y.sort()
print(y[0])

Result:
13) Python Program to Count the Number of Digits in a
Number
Code:
y=int(input("Enter any number: "))

res = [int(x) for x in str(y)]


z=len(res)
print(z)

Result:

14) Python Program to Check if a Number is a


Palindrome
Code:
x = input("Enter a number:")
if x == x[::-1]:
print("Yes, its a palindrome")
else:
print("No, its not a palindrome")

Result:

15) Python Program to Print all Integers that Aren't


Divisible by Either 2 or 3 and Lie between 1 and 50.
Code:
y=[]
for i in range(0,51):
if i%2!=0 and i%3!=0:
y.append(i)
print(y)

Result:

16) Python Program to Read a Number n And Print the


Series "1+2+…..+n= "
Code:
x=int(input("Enter a number: "))
y=[]

for i in range(0,x+1):
y.append(i)

print(sum(y))

Result:

17) Python Program to Read a Number n and Print the


Natural Numbers Summation Pattern
Code:
n=int(input("Enter a number: "))
for j in range(1,n+1):
a=[]
for i in range(1,j+1):
print(i,sep=" ",end=" ")
if(i<j):
print("+",sep=" ",end=" ")
a.append(i)
print("=",sum(a))

print()

Result:

18) Python Program to Print an Identity Matrix


Code:
n=int(input("Enter a number: "))
for i in range(0,n):
for j in range(0,n):
if(i==j):
print("1",sep=" ",end=" ")
else:
print("0",sep=" ",end=" ")
print()

Result:

19) Python Program to Print an Inverted Star Pattern


Code:
n=int(input("Enter number of rows: "))
for i in range (n,0,-1):
print((n-i) * ' ' + i * '*')
Result :

20) Python Program to Read Print Prime Numbers in a


Range using Sieve of Eratosthenes
Code:
n=int(input("Enter upper limit of range: "))
sieve=set(range(2,n+1))
while sieve:
prime=min(sieve)
print(prime,end="\t")
sieve-=set(range(prime,n+1,prime))
print()
Result:
21) Python Program to Check if a Date is Valid and
Print the Incremented Date if it is
Code:
date=input("Enter the date in dd/mm/yy
format: ")
dd,mm,yy=date.split('/')
dd=int(dd)
mm=int(mm)
yy=int(yy)
if(mm==1 or mm==3 or mm==5 or mm==7 or mm==8
or mm==10 or mm==12):
max1=31
elif(mm==4 or mm==6 or mm==9 or mm==11):
max1=30
elif(yy%4==0 and yy%100!=0 or yy%400==0):
max1=29
else:
max1=28
if(mm<1 or mm>12):
print("Date is invalid.")
elif(dd<1 or dd>max1):
print("Date is invalid.")
elif(dd==max1 and mm!=12):
dd=1
mm=mm+1
print("The incremented date is:
",dd,mm,yy)
elif(dd==31 and mm==12):
dd=1
mm=1
yy=yy+1
print("The incremented date is:
",dd,mm,yy)
else:
dd=dd+1
print("The incremented date is:
",dd,mm,yy)

Result:

22) Python Program to Compute Simple Interest Given


all the Required Values
Code:
principle=float(input("Enter the principle
amount:"))
time=int(input("Enter the time(years):"))

rate=float(input("Enter the rate:"))

simple_interest=(principle*time*rate)/100

print("The simple interest


is:",simple_interest)

Result:

23) Python Program to Check Whether a Given Year is


a Leap Year
Code:
year = int(input("Enter Year: "))

if year % 4 == 0 and year % 100 != 0:


print(year, "is a Leap Year")
elif year % 100 == 0:
print(year, "is not a Leap Year")
elif year % 400 ==0:
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")

Result:

24) Python Program to Read Height in Centimeters and


then Convert the Height to Feet and Inches
Code:
cm=int(input("Enter the height in
centimeters:"))
inches=0.394*cm
feet=0.0328*cm
print("The height in inches
is:",round(inches,2))
print("The height in feet
is:",round(feet,2))

Result:

25) Python Program to Take the Temperature in


Celcius and Covert it to Farenheit
Code:
celsius=int(input("Enter the temperature in
celcius:"))
f=(celsius*1.8)+32
print("Temperature in farenheit is:",f)
Result:

26) Python Program to Compute Prime Factors of an


Integer
Code:
n=int(input("Enter an integer:"))
print("Factors are:")
i=1
while(i<=n):
k=0
if(n%i==0):
j=1
while(j<=i):
if(i%j==0):
k=k+1
j=j+1
if(k==2):
print(i)
i=i+1

Result:
27) Python Program to Generate all the Divisors of an
Integer
Code:
number = int(input("Enter number: "))

print(f"Divisors are: {number}")


for i in range(1, number+1):
if number%i == 0:
print(i, end=" ")

Result:

28) Python Program to Print Table of a Given Number


Code:
num = int(input("Enter the number: "))

print("Multiplication Table of", num)


for i in range(1, 11):
print(num,"X",i,"=",num * i)

Result:
29) Python Program to Print Sum of Negative Numbers,
Positive Even Numbers and Positive Odd numbers in a
List
Code:
n=int(input("Enter the number of elements to
be in the list:"))
b=[]
for i in range(0,n):
a=int(input("Element: "))
b.append(a)
sum1=0
sum2=0
sum3=0
for j in b:
if(j>0):
if(j%2==0):
sum1=sum1+j
else:
sum2=sum2+j
else:
sum3=sum3+j
print("Sum of all positive even
numbers:",sum1)
print("Sum of all positive odd
numbers:",sum2)
print("Sum of all negative numbers:",sum3)

Result:

30) Python Program to Print Largest Even and Largest


Odd Number in a List
Code:
total_numbers = int(input("How my numbers
you want to add to the list : "))
numbers_array = []

for i in range(0,total_numbers):
numbers_array.append(int(input("Number in
list : ")))

largest_even = -1
largest_odd = -1

for i in range(0,total_numbers):
if(numbers_array[i] % 2 == 0 and
numbers_array[i] > largest_even):

largest_even = numbers_array[i]
elif(numbers_array[i] % 2 != 0 and
numbers_array[i] > largest_odd):

largest_odd = numbers_array[i]

print("Largest Odd Number : ",largest_odd)


print("Largest Even Number : ",largest_even)

Result:
31) Python Program to Form an Integer that has the
Number of Digits at Ten's Place and the Least
Significant Digit of the Entered Integer at One's Place
Code:
n=int(input("Enter the number:"))
tmp=n
k=0
while(n>0):
k=k+1
n=n//10
b=str(tmp)
c=str(k)
d=c+b[k-1]
print("The new number formed:",int(d))

Result:
32) Python Program to Find Those Numbers which are
Divisible by 7 and Multiple of 5 in a Given Range of
Numbers
Code:
a=int(input("Lower limit:"))
b=int(input("Upper limit:"))
nl=[]

for x in range(a, b):


if (x%7==0) and (x%5==0):
nl.append(str(x))
print (','.join(nl))

Result:

33) Python Program to Check if a Number is an


Armstrong Number
Code:
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Result:

34) Python Program to Print the Pascal's triangle for n


number of rows given by the user
Code:
n = int(input("Enter number of rows: "))
a = []

for i in range(n):
a.append([])
a[i].append(1)

for j in range(1, i):


a[i].append(a[i - 1][j - 1] + a[i -
1][j])
if (n != 0):
a[i].append(1)
for i in range(n):
print(" " * (n - i), end = " ", sep =
" ")

for j in range(0, i + 1):


print('{0:5}'.format(a[i][j]), end =
" ", sep = " ")
print()

Result:

35) Python Program to Check if a Number is a Perfect


Number
Code:
Number = int(input(" Please Enter any
Number: "))
Sum = 0
for i in range(1, Number):
if(Number % i == 0):
Sum = Sum + i
if (Sum == Number):
Practical-3
AIML
print(" %d is a Perfect Number" %Number)
else:
print(" %d is not a Perfect Number"
%Number)

Result:

36) Python Program to Check if a Number is a Strong


Number
Code:
sum=0
num=int(input("Enter a number:"))
temp=num
while(num):
i=1
fact=1
rem=num%10
while(i<=rem):
fact=fact*i
i=i+1
sum=sum+fact
num=num//10
if(sum==temp):
print("Given number is a strong number")
else:
print("Given number is not a strong
number")
Result:

37) Python Program to Find the LCM of Two Numbers


Code:
num1 = int(input("Enter the first number:"))
num2 = int(input("Enter the second
number:"))
if(num1 > num2 ):
min1=num1
else:
min1= num2
while(1):
if(min1 % num1 == 0 and min1 % num2 ==
0):
print("LCM is: ",min1)
break;
min1= min1+ 1

Result:

38) Python Program to Find the GCD of Two Numbers


Code:
num1 = int(input("Enter 1st number: "))
num2 = int(input("Enter 2nd number: "))
i = 1
while(i <= num1 and i <= num2):
if(num1 % i == 0 and num2 % i == 0):
gcd = i
i = i + 1
print("GCD is", gcd)

Result:

39) Python Program to Compute a Polynomial Equation


given that the Coefficients of the Polynomial are stored
in a List
Code:
import math
print("Enter the coefficients of the form
ax^3 + bx^2 + cx + d")
lst=[]
for i in range(0,4):
a=int(input("Enter coefficient:"))
lst.append(a)
x=int(input("Enter the value of x:"))
sum1=0
j=3
for i in range(0,3):
while(j>0):
sum1=sum1+(lst[i]*math.pow(x,j))
break
j=j-1
sum1=sum1+lst[3]
print("The value of the polynomial
is:",sum1)

Result:

40) Python Program to Check If Two Numbers are


Amicable Numbers
Code:
x=int(input("Enter 1st number:"))
y=int(input("Enter 2nd number:"))

sum_x=0
sum_y=0

for each in range(1,x):


if(x%each==0):
sum_x=sum_x+each
for i in range(1,y):
if(y%i==0):
sum_y=sum_y+i
if(sum_x==y and sum_y==x):
print("They are Amicable numbers")
else:
print("They are not Amicable")

Result:

41) Python Program to Find the Area of a Triangle


Given All Three Sides
Code:
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))

s = (a + b + c) / 2

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5


print('The area of the triangle is %0.2f'
%area)
Result:

42) Python Program to Find the Gravitational Force


Acting Between Two Objects
Code:
G = 6.673*(10**-11)

m1 = float(input('Enter weight of first body


(Kg): '))
m2 = float(input('Enter weight of second
body (Kg): '))
r = float(input('Enter distance (m): '))

F =(G * m1 * m2) /(r**2)

print('Gravitational Force = %f Newton'


%(F))

Result:
43) Python Program to Check if a Number is a Prime
Number
Code:
a=int(input("Enter a number:"))
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print("Number is prime.")
else:
print("Number is not prime.")

Result:

44) Python Program to Print all the Prime Numbers


within a Given Range
Code:
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)

Result:

45) Python Program to Print Numbers in a Range


(1,upper) Without Using any Loops
Code:
def printNos(n):
if n > 0:
printNos(n - 1)
print(n)
n=int(input("Upper limit:"))
printNos(n)

Result:

46) Python Program to Find the Sum of Sine Series


Code:
import math
def sin(x, n):
sine = 0
for i in range(n):
sign = (-1)**i
pi = 22/7
y = x*(pi/180)
sine =sine +
((y**(2.0*i+1))/math.factorial(2*i+1))*sign
return sine
x =int(input("Enter the value of x in
degrees:"))
n =int(input("Enyer the number of terms:"))
print(round(sin(x,n),2))

Result:
47) Python Program to Find the Sum of First N Natural
Numbers
Code:
number=int(input("Enter a number: "))
sum = 0
while(number > 0):
sum+=number
number-=1
print("The sum of first n natural numbers
is",sum)

Result:

48) Python Program to Find the Sum of the Series: 1 +


1/2 + 1/3 + ….. + 1/N
Code:
number=int(input("Enter the number of terms:
"))
sum=0
for i in range(1,number+1):
sum+=+(1/i)
print("The sum of series is",round(sum,2))

Result:
49) Python Program to Find the Sum of the Series: 1 +
x^2/2 + x^3/3 + … x^n/n
Code:
n=int(input("Enter the number of terms:"))
x=int(input("Enter the value of x:"))
sum1=1
for i in range(2,n+1):
sum1=sum1+((x**i)/i)
print("The sum of series is",round(sum1,2))

Result:

50) Python Program to Compute the Value of


Euler's Number e. Use the Formula: e = 1 + 1/1! +
1/2! + …… 1/n!
Code:
import math
n=int(input("Enter the number of terms:"))
sum1=1
for i in range(1,n+1):
sum1=sum1+(1/math.factorial(i))
print("The sum of series is",round(sum1,2))

Result:

You might also like