[go: up one dir, main page]

0% found this document useful (0 votes)
2 views8 pages

python

The document contains multiple Python programs written by Vasu Mittal, showcasing various functionalities such as checking for palindromes, swapping list elements, finding the second largest number, and calculating factorials. Each program is accompanied by its source code and expected output. The programs demonstrate fundamental programming concepts and operations on lists and numbers.

Uploaded by

arjit Kumar
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)
2 views8 pages

python

The document contains multiple Python programs written by Vasu Mittal, showcasing various functionalities such as checking for palindromes, swapping list elements, finding the second largest number, and calculating factorials. Each program is accompanied by its source code and expected output. The programs demonstrate fundamental programming concepts and operations on lists and numbers.

Uploaded by

arjit Kumar
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/ 8

Name - Vasu Mittal

University roll no. 2222148


Roll no.- 69
Program 1: To check the number is palindrome or not

Source code:

a=int(input("enter a number"))
t=a
p=0
while a>0:
n=a%10
p=p*10+n
a=a/10
if t==p:
print("number is
palindrome") else:
print("number is not palindrome")

Output:
Program 2:Program to swap two elements from the list

Source code:

a=[1,2,3,4,5,6]
print("values before swap",a)
t=a[4]
a[4]=a[0]
a[0]=t
print("values after swap",a)

Output:
Program 3:To find the second largest number

Source code:

a=[10,20,30,40,50]
b=sorted(a,reverse=True)
sl=b[1]
print(a)
print("second largest in the list is",sl)

Output:
Program 4: To find the index of an item

Source code:

a=[2,3,4,5,7,6,9]
b=a.index(5)
print(a)
print("index of number is",b)

Output:
Program 5: Wap to find the sum and average of list items

Source Code:

L = [4, 5, 1, 2, 9, 7, 10, 8]
count = 0
for i in
L:
count += i
avg = count/len(L)
print("sum = ", count)
print("average = ", avg)

Output:
Program 6: Consider a number entered by a user.Now calculate the factorial of this number.

Source Code:

num = 7
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is
1") else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output:
Program 7: Wap to print the fibonacci sequence upto the range entered by user.

Source Code:

nterms = int(input("How many terms? "))


n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive
integer") elif nterms == 1:
print("Fibonacci sequence
upto",nterms,":") print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output:
Program 8: Wap to check whether a number entered by the user is a perfect number or not.

Source Code:

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


sum_v=0
for i in range(1,num):
if (num%i==0):
sum_v=sum_v+i
if(sum_v==num):
print("The entered number is a perfect
number") else:
print("The entered number is not a perfect number")

Output:

You might also like