[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

Sample Programs

The document contains a series of Python programming exercises designed for Class IX students at Delhi Public School-Bopal, Ahmedabad. It covers various topics including loops, string manipulation, and list operations, with specific tasks such as calculating sums, checking for prime numbers, reversing strings, and generating Fibonacci series. Each exercise includes a code snippet that demonstrates the required functionality.

Uploaded by

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

Sample Programs

The document contains a series of Python programming exercises designed for Class IX students at Delhi Public School-Bopal, Ahmedabad. It covers various topics including loops, string manipulation, and list operations, with specific tasks such as calculating sums, checking for prime numbers, reversing strings, and generating Fibonacci series. Each exercise includes a code snippet that demonstrates the required functionality.

Uploaded by

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

DELHI PUBLIC SCHOOL-BOPAL, AHMEDABAD

Class: IX Subject: Artificial Intelligence Session: 2024-2025


Sample programs based on Python loops, string and list

1. WAP to display the sum of first ‘n’ numbers using loop.


n=int(input("Enter how many numbers:"))
s=0
for i in range(n):
s=s+i
print("Sum of ", n, "numbers=",s)

2. WAP to display the factorial of ‘n’ using loop.


n=int(input("Enter how many numbers:"))
fact=1
for i in range(1,n+1):
fact=fact*i
print("Factorial of ", n, "=",fact)

3. WAP to check whether the given number is prime or not.


n=int(input("Enter a number:"))
for i in range(2,n):
if n%i==0:
print(n, "is not a prime number")
break
else:
print(n, "is a prime number")

4. WAP to reverse a number using a numeric value.


n=int(input("Enter a number:"))
n1=n
rev=0
while n1!=0:
d=n1%10
rev=rev*10+d
n1=n1//10
print("Reverse of ",n, " = ", rev)

5. WAP to check whether the given number is Armstrong or not. (An Armstrong number
is a number that is equal to the sum of cube of its own digits. For example 153,
1*1*1+5*5*5+3*3*3=153.
n=int(input("Enter a number:"))
n1=n
arm=0
while n1!=0:
d=n1%10
arm=arm+(d**3)
n1=n1//10
if arm==n:
print(n , "is an Armstrong number")
else:
print(n , "is not an Armstrong number")
6. WAP to check whether the given number is perfect number or not. Perfect number is a
positive integer that is equal to the sum of its positive proper divisors excluding the
number itself. For example, factors of 6 is 1, 2, 3. Sum of 1+2+3=6.
n=int(input("Enter a number:"))
factor=1
for i in range(2,n):
if n%i==0:
factor=factor+i
if factor==n:
print(n , "is a perfect number")
else:
print(n , "is not a perfect number")

7. WAP to reverse a string using loop and without using string functions.
str1=input("Enter a string:")
str2=""
for i in str1:
str2=i+str2
print("Given String: ",str1)
print("Reversed String:", str2)

8. WAP to check whether the given string is a palindrome or not without using string
functions.
str1=input("Enter a string:")
str2=""
for i in str1:
str2=i+str2
if str1==str2:
print(str1, "is a palindrome")
else:
print(str1, "is not a palindrome")

9. WAP to count the length of characters in a string without using len() function.
str1=input("Enter a string:")
count=0
for i in str1:
count+=1
print("Length of the string: ", count)

10. WAP to count the number of alphabets, numbers & special characters present in a
given string without using string functions.
str1=input("Enter a string:")
al,no,sp=0,0,0
for i in str1:
if (i>= "A" and i<= "Z") or (i>= "a" and i<= "z"):
al+=1
elif (i>= "0" and i<= "9"):
no+=1
else:
sp+=1
print("No of alphabets of the string: ", al)
print("No of digits: ", no)
print("No of special characters: ", sp)
11. WAP to count the number of lowercase letters, uppercase letters & digits in a given
string without using string functions.
str1=input("Enter a string:")
lc,uc,no=0,0,0
for i in str1:
if (i>= "A" and i<= "Z"):
uc+=1
elif (i>= "a" and i<= "z"):
lc+=1
elif (i>= "0" and i<= "9"):
no=no+1
print("No of Uppercase letters:", uc)
print("No of Lowercase letters:", lc)
print("No of digits:", no)

12. WAP to count the presents of vowels, consonants in a string without using string
functions.
str1=input("Enter a string:")
vo,co=0,0
vowels=["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]
for i in str1:
if i in vowels:
vo+=1
elif (i>= "a" and i<="z") or (i>= "A" and i<="Z" ):
co+=1
print("No of Vowels ", vo)
print("No of Consonants: ", co)

13. WAP to convert uppercase letters to lowercase letters and lowercase letters to
uppercase letters in a string without using string functions. Keep rest of the characters
as it is.
str1=input("Enter a string:")
str2= ""
for i in str1:
if i>= "a" and i<= "z":
str2=str2+chr(ord(i)-32)
elif i>= "A" and i<= "Z" :
str2=str2+chr(ord(i)+32)
else:
str2=str2+i

print("Given String:",str1)
print("Toggled String:", str2)

14. WAP to create ‘n’ terms of Fibonacci Series (0,1,1,2,3,5,8,13,…..) without using list
n=int(input("Enter no. of terms:"))
n1=0
n2=1
n3=0
for i in range(n):
print(n3, end= " ")
n1=n2
n2=n3
n3=n1+n2

15. WAP to create ‘n’ terms of Fibonacci Series (0,1,1,2,3,5,8,13,…..) using list
n=int(input("How many numbers:"))
fibo=[0,1]
for i in range(2,n):
next=fibo[i-1]+fibo[i-2]
fibo.append(next)
print(fibo)

16. WAP to find the sum of all numbers in a list. Get numeric list from the user.
n=int(input("Enter no. numeric values:"))
list1=[]
sum=0
for i in range(n):
item=int(input("Enter a number:"))
list1.append(item)
sum=sum+item11
print(list1)
print("Some of all numbers:", sum)

17. WAP to reverse the list using loop without list function. Get numeric list from the
user.
n=int(input("Enter no. of terms:"))
list1=[]
for i in range(n):
item=int(input("Enter a number:"))
list1.append(item)
print("Given List:",list1)
mid=n//2
lastindex=n-1
for i in range(mid):
temp=list1[i]
list1[i]=list1[lastindex]
list1[lastindex]=temp
lastindex=lastindex-1
print("Reversed11 List:",list1)

18. WAP to find the largest and smallest number in a numeric list. Get content of the list
from user.
n=int(input("Enter no. of terms:"))
list1=[]
for i in range(n):
item=int(input("Enter a number:"))
list1.append(item)
print("Given List:",list1)
large=list1[0]
small=list1[0]
for i in range(n):
if list1[i]>large:
large=list1[i]
if list1[i]<small:
small=list1[i]
print("Largest element:",large)
print("Smallest element:", small)

You might also like