Ai Programs
Ai Programs
Aim:
To find the factorial for given number using while loop.
Program:
#Python program find factorial of a number
#using while loop
while(num>0):
factorial=factorial*num
num=num-1
Output:
Enter a number to find factorial:
120
Program:
n=int(input("Enter the number of series:"))
first,second = 0,1
print("Fibonacci series")
for i in range(2,n):
third=first+second
print(third,end=' ')
first,second=second,third
Output:
Enter the number of series:
Fibonacci series
01123
Coding:
#Take the input from the user:
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Output:
Enter lower range:
15
11
13
for i in string:
str1 = i + str1
print("String in reverse Order : ", str1)
if(string == str1):
print("This is a Palindrome String")
else:
print("This is Not a Palindrome String")
Output:
Please enter your own String :
madam
k=0
while k!=(2*i-1):
print("* ", end="")
k += 1
k=0
print()
Output:
Enter number of rows:
10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
Coding:
str1='0123abcdEFG@@@'
digit=upper=lower=0
special=alpha=0
for x in str1:
if x.isalnum():
if x.isalpha():
alpha += 1
if x.isupper():
upper+=1
else:
lower+=1
else:
digit+=1
else:
special+=1
print("digit is:",digit)
print("uppercase is:",upper)
print("lowercase is:",lower)
print("alphabet is:",alpha)
print("special character is:",special)
Output:
digit is: 4
uppercase is: 3
lowercase is: 4
alphabet is: 7
special character is: 3
7. Program to draw bar chart
Coding:
import matplotlib.pyplot as pl
year=['2015','2016','2017','2018','2019']
p=[98.5,70.2,55.6,90.5,61.5]
c=['b','g','r','m','c']
pl.bar(year,p,width=0.5,color=c)
pl.title("BARCHART")
pl.xlabel("Year")
pl.ylabel("Profit%")
pl.show()
Output:
8. Program to plot a Chart for y=x2
Coding:
import matplotlib.pyplot as pl
import numpy as np
pl.title("LINE GRAPH")
pl.xlabel("x axis")
pl.ylabel("y axis")
x=np.linspace(-50,50)
y=x**2
pl.plot(x,y,linestyle="dotted")
pl.show()
Output:
9. Program to draw a pie chart
Coding:
import matplotlib.pyplot as plt
runs = [77,103,42,22]
players = ['Rohit sharma','MS dhoni','Jasmit bumrah','Virat kohli']
cols = ['c','m','r','b']
plt.pie(runs,labels=players,colors=cols,startangle=90,shadow=True,explode=(
0,
0.0,0,0.1),autopct='%1.1f%%')
plt.title('Runs Scored Chart')
plt.show()
Output:
10. Basic Array operations in Numpy using for loop
Coding:
import numpy as np
print("Array1:")
for i in a1 :
print(i)
print("Array2:")
for j in a2:
print(j)
print("Sum of two arrays")
sum=np.add(a1,a2)
print(sum)
Output:
Array1:
[1 2 3]
[4 5 6]
[2 4 2]
[6 8 9]
Array2:
[1 1 1]
[2 3 4]
[2 4 2]
[2 2 2]
Sum of two arrays
[[ 2 3 4]
[ 6 8 10]
[ 4 8 4]
[ 8 10 11]]
11. Date and Time Operation in Python
Coding:
import datetime as dt
import calendar
Output:
2022-02-05 08:19:42.335316
08:19:42.335517
Month: 2
Year: 2022
Day of Month: 5
Day of Week (number): 5
Day of Week (name): Saturday
12. Python program for various math function
Coding:
import math
#finding squareroot
#finding power
#finding gcd
pi=math.pi
area=pi*r*r
Output:
Enter a decimal value to find its floor & ceiling value:5
The Ceiling of the 5 = 5
The Floor of the 5 = 5
Enter a value to find its squareroot:4
Square root of 4 = 2.0
The value of 5^8: 390625.0
Enter number 1:12
Enter number 2:34
The GCD of: 12 34 = 2
Enter radius of circle:5
Area of circle is: 78.53981633974483