[go: up one dir, main page]

0% found this document useful (0 votes)
28 views13 pages

Ai Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views13 pages

Ai Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1) Factorial Number using While Loop

Aim:
To find the factorial for given number using while loop.
Program:
#Python program find factorial of a number
#using while loop

num=int(input("Enter a number to find factorial: "))


#takes input from user

factorial=1; #declare and initialize factorial variable to one

#check if the number is negative ,positive or zero


if num<0:
print("Factorial does not defined for negative integer");
elif(num==0):
print("The factorial of 0 is 1");
else:

while(num>0):
factorial=factorial*num

num=num-1

print("factorial of the given number is: ")


print(factorial)

Output:
Enter a number to find factorial:

factorial of the given number is:

120

** Process exited - Return Code: 0 **

Press Enter to exit terminal


2) Fibonacci Series using for loop
Aim:
To find the Fibonacci series for given number using for loop.

Program:
n=int(input("Enter the number of series:"))

first,second = 0,1

print("Fibonacci series")

print(first,second,end=' ') #first 2 nos of 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

** Process exited - Return Code: 0 **

Press Enter to exit terminal


3) Finding Prime Numbers between given Limits
Aim:
To find the prime numbers between given Limits

Coding:
#Take the input from the user:

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)

Output:
Enter lower range:

Enter upper range:

15

11

13

** Process exited - Return Code: 0 **

Press Enter to exit terminal


4) String Palindrome
Aim:
To find the given string is palindrome or not
Coding:
# Python Program to Check a Given String is Palindrome or Not

string = input("Please enter your own String : ")


str1 = ""

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

String in reverse Order : madam

This is a Palindrome String

** Process exited - Return Code: 0 **

Press Enter to exit terminal


5. Simple Pyramid Pattern (Asterisk Symbol)
Aim:
To find the simple pyramid pattern in python
Coding:
rows = int(input("Enter number of rows: "))

k=0

for i in range(1, rows+1):


for space in range(1, (rows-i)+1):
print(end=" ")

while k!=(2*i-1):
print("* ", end="")
k += 1

k=0
print()
Output:
Enter number of rows:
10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************

** Process exited - Return Code: 0 **


Press Enter to exit terminal
6. Counting alphabets, digits, upper case, lower case
and special characters in a string.

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

a1 = np.array([[1, 2, 3], [4, 5, 6], [2,4,2], [6,8,9]])


a2 = np.array([[1, 1, 1], [2, 3, 4], [2, 4, 2], [2, 2, 2]])

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

#To print date and time


my_date= dt.datetime.now()
print(my_date)

#To print time alone


my_time= dt.datetime.now().time()
print(my_time)

# To get month from date


print('Month: ', my_date.month)

# To get month from year


print('Year: ', my_date.year)

# To get day of the month


print('Day of Month:', my_date.day)

# to get name of day(in number) from date


print('Day of Week (number): ', my_date.weekday())

# to get name of day from date


print('Day of Week (name): ', calendar.day_name[my_date.weekday()])

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

val1=eval(input("Enter a decimal value to find its floor & ceiling value:"))

print('The Ceiling of the',val1,"=" ,math.ceil(val1))

print('The Floor of the',val1,"=", math.floor(val1))

#finding squareroot

val2=eval(input("Enter a value to find its squareroot:"))

print('Square root of ',val2,"=", math.sqrt(val2))

#finding power

print('The value of 5^8: ',math.pow(5, 8))

#finding gcd

a1=int(input("Enter number 1:"))

a2=int(input("Enter number 2:"))

print('The GCD of:',a1,a2,'=',math.gcd(a1,a2))

#finding area of circle using pi value

r=eval(input("Enter radius of circle:"))

pi=math.pi

area=pi*r*r

print("Area of circle is:",area)

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

You might also like