0% found this document useful (0 votes)
78 views7 pages

Python Lab Exercises: Code

The document contains Python code examples for common programming exercises including calculating factorials, finding squares of list elements, printing odd numbers using ranges and while loops, calculating series, generating multiplication tables, finding factors of numbers, checking for prime numbers, calculating sums if numbers are within a range, reversing digits of a number, and finding the sum of digits in a number. The code provides solutions to these problems using basic Python constructs like for loops, if/else statements, arithmetic and logical operators.

Uploaded by

ram gopal sharma
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)
78 views7 pages

Python Lab Exercises: Code

The document contains Python code examples for common programming exercises including calculating factorials, finding squares of list elements, printing odd numbers using ranges and while loops, calculating series, generating multiplication tables, finding factors of numbers, checking for prime numbers, calculating sums if numbers are within a range, reversing digits of a number, and finding the sum of digits in a number. The code provides solutions to these problems using basic Python constructs like for loops, if/else statements, arithmetic and logical operators.

Uploaded by

ram gopal sharma
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/ 7

Python Lab Exercises

Q1 WAP to find factorial of a given number

Code:
num=int(input("Enter a number: "))
fact=1
if num<0:
print("Sorry, factorial doesn't exist for negative numbers")
elif num==0:
print("Factorial of 0 is 1")
else:
for i in range(1,num+1):
fact=fact*i
print("The factorial of ",num,"is ",fact)

Output:

Q2 WAP to print square of all numbers present in a list

Code:
num=[1,2,4,6,11,20]
sq=0
for val in num:
sq=val*val
print (sq)

Output:

Q3WAP to print first 50 odd numbers


a) using Range()
Code:
i=0
for i in range(1,100,2):
print(i)

Output:

b) without using range

Code:
n=int(input("Enter your maximum value "))
i=1
while(i<=n):
if i%2!=0:
print(i)
i =i+1

Output:

Q4 WAP to generate result of following series


a) x = 1+2+3+4+5+......
Code:
num=int(input ("Enter end of series: "))
sum=0
for i in range(1,num+1):
sum=sum+i
print (sum)

Output:

b) y = 1+2-3+4-5+......
Code:
num=int(input ("Enter end of series: "))
sum=0
for i in range(1,num+1):
if i%2==0:
sum=sum-i
else:
sum=sum+i
print (sum)

Output:

b) 1-2/2!+3/3!....n/n!.
Code:
n=int(input("Enter a number: "))
sum=0
fact=1
for i in range(1,n+1):
fact *=i
if i%2==0:
sum=sum-(i/fact)
else:
sum=sum+(i/fact)
print(sum)
Output:

d) 1^k+2^k+…+n^k where n and k are entered by the user.

Code:
n=int(input('enter a number '))
k=int(input('enter a number '))
sum=0
for i in range(1,n+1):
sum=sum+pow(i,k)
print(sum)

Output:

Q5. WAP to print:


a) The multiplication table for a number entered by the user.

Code:
num=int(input("Enter a number: "))
print("Multiplication table of" ,num)
for i in range(1,11):
print(num,"x",i,"=",num*i)

Output:

b) The multiplication table for all the numbers between 1 and 9.

Code:
n=int(input("Enter a number: "))
for i in range(1,n+1):
print("Multiplication of ",i)
for s in range(1,11):
print(i,"x",s,"=",i*s)

Output:
Q6. WAP
a) To find the factors of a number.

Code:
print("Finding factors using for loop")
n=int(input("Enter a number: "))
print("The factors of ",n,"are ")
for i in range(1,n+1):
if n%i==0:
print(i)

print("Finding factors using while loop")


n=int(input("Enter a number: "))

i=1

while(i<=n):

if n%i==0:

print(i)

i +=1

Output:
b) To find out whether a no is prime or not.
Using (for loop and while loop).

Code:
print("Determining prime numbers using for loop")
n=int(input("Enter a number: "))
if(n==1):
print(n,"nor prime nor composite")
else:
for i in range(1,n+1):
if (n%i)==0:
print(n,"is not a prime number")
break
else:
print(n,"is not a prime number")

print("Determining prime numbers using while loop")


n=int(input("Enter a number: "))
if n==1:
print(n,"is not prime and nor composite ")
else:
i=1
while(i<=n):
if (n%i)==0:
print(n,'is not a prime number')
break
else:
print(n,'is a prime number')

Output:
Q7. WAP to calculate sum of numbers only if numbers are between 1 and 100.

Code:
n=int(input("Enter any number: "))
j=int(input("Enter any other number:c"))
sum=0
if (n and j)>=1:
if n<=100 and j<=100:
sum=n+j
print(sum)
else:
print("Invalid value")

Output:

Q8. WAP to reverse the digits of a number.

Code:
n=int(input("Enter a number: "))
reverse=0
while(n):
reverse= reverse*10
reverse=reverse + n%10
n=n//10
print ("Reverse of entered number is ",reverse)

Output:

Q9. WAP to find sum of digits of a number.

Code:
num=int(input ("Enter any number: "))
sum=0
while(num>0):
rem=num%10
sum=sum+rem
num=num//10
print("Sum of digits of given number is: ",sum)

Output:

You might also like