0% found this document useful (0 votes)
69 views6 pages

X Ai Practicals 2022-23

This document contains summaries of 13 programming practicals involving tasks like number swapping, arithmetic calculations, checking leap years, finding largest of 3 numbers, calculating sum of even/odd numbers, digit sum, checking vowels/consonants, month name from number, number tables, prime checking, and pattern generation using nested loops. Each practical provides the objective, source code, and example outputs to demonstrate the program's functionality.

Uploaded by

tokitouchiwa97
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)
69 views6 pages

X Ai Practicals 2022-23

This document contains summaries of 13 programming practicals involving tasks like number swapping, arithmetic calculations, checking leap years, finding largest of 3 numbers, calculating sum of even/odd numbers, digit sum, checking vowels/consonants, month name from number, number tables, prime checking, and pattern generation using nested loops. Each practical provides the objective, source code, and example outputs to demonstrate the program's functionality.

Uploaded by

tokitouchiwa97
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/ 6

KENDRIYA VIDYALAYA SANGATHAN

CLASS-X SUBJECT – ARTIFICIAL INTELLIGENCE(417)


PRACTICAL FILE SUGGESTIVE SOLUTIONS (SESSION 2022-23)
PRACTICAL NO: 1
OBJECTIVE Write a program to input two numbers and swap them.
SOURCE Num1=int(input("Enter Number-1: "))
CODE: Num2=int(input("Enter Number-2: "))
print("Before Swap: ")
print("Num1: ", Num1)
print("Num2: ", Num2)

Num1, Num2= Num2, Num1


print("After Swap:")
print("Num1: ", Num1)
print("Num2: ", Num2)

OUTPUT: Enter Number-1: 10


Enter Number-2: 20
Before Swap:
Num1: 10
Num2: 20
After Swap:
Num1: 20
Num2: 10

PRACTICAL NO: 2
OBJECTIVE Write a program to input three numbers and swap them as this: 1st number becomes 2nd number 2nd
number becomes 3rd number and 3rd number becomes 1st number.
SOURCE Num1=int(input("Enter Number-1: "))
CODE: Num2=int(input("Enter Number-2: "))
Num3=int(input("Enter Number-3: "))
print("Before Swap: ")
print("Num1: ", Num1)
print("Num2: ", Num2)
print("Num3: ", Num3)

Num1, Num2,Num3= Num2, Num3,Num1


print("After Swap:")
print("Num1: ", Num1)
print("Num2: ", Num2)
print("Num3: ", Num3)

OUTPUT: Enter Number-1: 10


Enter Number-2: 20
Enter Number-3: 30
Before Swap:
Num1: 10
Num2: 20
Num3: 30
After Swap:
Num1: 20
Num2: 30
Num3: 10

PRACTICAL NO: 3
OBJECTIVE Write a program that reads two numbers and an arithmetic operator and displays the computed result.
SOURCE a=float(input('Enter the first number: '))
CODE: b=float(input('Enter the second number: '))
c=input('Enter the operator[/,*,+,-]: ')
if c=='/':
r=a/b
print(a,c,b,'=',r)
elif c=='*':
r=a*b
print(a,c,b,'=',r)
elif c=='+':
r=a+b
print(a,c,b,'=',r)
elif c=='-':
r=a-b
print(a,c,b,'=',r)
else:
print('Invalid operator')

OUTPUT: Enter the first number: 8


Enter thesecond number: 2
Enter the operator[/,*,+,-]: /
8.0 / 2.0 = 4.0

PRACTICAL NO: 4
OBJECTIVE Write a program to accept the year and check if it is a leap year or not.
SOURCE a=int(input('Enter the year:'))
CODE: if a%4==0:
print('This year is a leap year')
else:
print('This year is not a leap year')

OUTPUT: Enter the year: 2020


This year is a leap year

PRACTICAL NO: 5
OBJECTIVE Write a program to find largest among three integers. Make use of if -else statement.
SOURCE a=int(input('Enter the first integer:'))
CODE: b=int(input('Enter the second integer:'))
c=int(input('Enter the third integer:'))
if a>b and a>c:
print(a, 'is the largest integer')
if b>a and b>c:
print(b, 'is the largest integer')
if c>a and c>b:
print(c, 'is the largest integer')
else:
print(‘All or any two are equal')
OUTPUT: Enter the first integer:56
Enter the second integer:34
Enter the third integer:67
67 is the largest integer.

PRACTICAL NO: 6
OBJECTIVE Write a program to calculate and print the sum of even and odd integers of the first n natural numbers.
SOURCE Even = int(input("Enter the input"))
CODE: total = 0
for number in range(1, Even+1):
if(number % 2 == 0):
print(number)
total = total + number
print("The sum of even numbers", total)

Odd = int(input("Enter the input"))


total = 0
for number in range(1, Odd+1):
if(number % 2!=0):
print(number)
total = total + number
print("The sum of odd numbers", total)
OUTPUT:

PRACTICAL NO: 7
OBJECTIVE Write a program to input a number and print the sum of digits in the given number.
SOURCE n = int(input("Enter a Number : "))
CODE: sum = 0
while (n != 0):
sum = sum + (n % 10)
n = n//10
print(sum)

OUTPUT: Enter a Number : 123


The sum of the number is : 6

PRACTICAL NO: 8
OBJECTIVE Write a program to accept a character from the user and display whether it is vowel or
consonant.
SOURCE ch=input("Please enter the character as you wish: ")
CODE: if(ch== 'a'or ch== 'A'or ch== 'e'or ch== 'E'or ch== 'i' or ch=='I' or ch=='o' or ch=='O' or ch=='u' or ch=='U'):
print("The given character ",ch," is the vowel")
else:
print("The given character ",ch," is the consonant")

OUTPUT: Please enter the character as you wish: A


the given character is a vowel

Please enter the character as you wish: s


the given character is a consonant

PRACTICAL NO: 9
OBJECTIVE
SOURCE x = int(input("Enter the month number: "))
CODE: if (x==1):
print ("January")
if (x==2):
print("February")
if (x==3):
print("March")
if (x==4):
print("April")
if (x==5):
print("May")
if (x==6):
print("June")
if (x==7):
print("July")
if (x==8):
print("August")
if(x==9):
print("September")
if(x==10):
print("October")
if(x==11):
print("November")
if(x==12):
print("December")
if(x<1 or x>12):
print("Invalid input")

OUTPUT: Case 1:
Enter the month number: 1
Jan
Case 2:
Enter the month number: 6
June
Case 3:
Enter the month number: 13
Invalid input
Case 4:
Enter the month number: -10
Invalid input

PRACTICAL NO: 10
OBJECTIVE Write a program to input a number and display the table of that number.
SOURCE n=int(input("Enter The Number : "))
CODE: i=1
while(i<=10):
t=n*i
print(n,"x",i,"=",t)
i=i+1

OUTPUT: Enter The Number : 7


7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

PRACTICAL NO: 11
OBJECTIVE Write a program to enter a number and check if its prime or not.
SOURCE n = int(input("Enter The Number : "))
CODE: # Check if the number is greater than 1
if n > 1:
for i in range(2, int(n/2)+1):
if (n % i) == 0:
print(num, "is not a prime number")
break
else:
print(n, "is a prime number")
# If the number is less than 1, its also not a prime number.
else:
print(n, "is not a prime number")
OUTPUT:
5 is a prime number

PRACTICAL NO: 12
OBJECTIVE Write a Program to generate the following patterns using nested loops.
SOURCE row = int(input("Enter the number of rows : "))
CODE: print("Pattern -1 ")
for i in range(0, row + 1, 1):
for j in range(1, i + 1):
print('*', end=' ')
print("")

print("Pattern -2 ")
for i in range(0, row + 1, 1):
for j in range(1, 5-i+1 ):
print(j, end=' ')
print("")

print("Pattern -3 ")
for i in range(0, row + 1, 1):
for j in range(65, 65+i ):
print(chr(j), end=' ')
print("")
OUTPUT: Pattern -1 Pattern -2 Pattern -3
Enter the number of rows : 5 Enter the number of rows : 5 Enter the number of rows : 5
12345
* 1234 A
** 123 AB
*** 12 ABC
**** 1 ABCD
***** ABCDE

PRACTICAL NO: 13
OBJECTIVE Write a program to find sum of series : s=1+x+x²+x³+x⁴…+xⁿ
SOURCE x=float(input('Enter the value of x: '))
CODE: n=int(input('Enter the value of n (for x**n): '))
s=0
for a in range(n+1):
s+=x**a

print('Sum of first' ,n,'terms: ',s)


OUTPUT: Enter the value of x: 3
Enter the value of n (for x**n): 6
Sum of first 6 terms: 1093.0

PRACTICAL NO: 14
OBJECTIVE Write a program in python to compute the Greatest Common Divisor and Least Common Multiple of two
integers.
SOURCE # GCD PROGRAM
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("Greatest Common Divisor (GCD) is ", gcd)

# LCM PROGRAM
if num1 > num2:
greater = num1
else:
greater = num2
while(True):
if((greater % num1 == 0) and (greater % num2 == 0)):
lcm = greater
break
greater += 1
print("The Least Common Multiple (LCM) is ", lcm)
OUTPUT: Enter 1st number: 3
Enter 2nd number: 5
Greatest Common Divisor (GCD) is 1
The Least Common Multiple (LCM) is 15

PRACTICAL NO: 15
OBJECTIVE Write a program in python to visualization plots bar and pie chart using matplotlib library.
[Hint: first from cmd prompt> pip install matplotlib]
SOURCE # Import libraries
CODE: from matplotlib import pyplot as plt
import numpy as np

# Creating dataset
cars = ['AUDI', 'BMW', 'FORD', 'TESLA', 'JAGUAR', 'MERCEDES']
data = [23, 17, 35, 29, 12, 41]

# Creating plot
fig = plt.figure(figsize =(10, 7))
plt.pie(data, labels = cars)

# show pie plot


plt.show()

plt.bar(cars, data)
# show pie plot
plt.show()
OUTPUT:

You might also like