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

WAP Examples for Basic Calculations

Uploaded by

Kisna Garg
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)
97 views6 pages

WAP Examples for Basic Calculations

Uploaded by

Kisna Garg
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

Q1.

WAP to calculate the area of a rectangle


def area(l,b):
a = l*b
return a
l = int(input("Enter the length:",))
b = int(input("Enter the breadth:",))
ar = area(l,b)
print("Area of rectangle:",ar)

Q2. WAP to find the smallest value from the list of n numbers.
def smallest(l1):
t2 = l1[0]
for t1 in l1:
if t2>t1:
t2 = t1
return t2
l = []
a = int(input("Enter Number:",))
for i in range(a):
b = int(input("Enter the number to enter:",))
[Link](b)

sm = smallest(l)

print("smallest number:",sm)

Q3. WAP to calculate the greatest common divisor of two


numbers.
ANS CODE:
def f(a,b):
d=1
if a!=0 and b!=0:
if a>b:
c=b
else:
c=a
for d in range (1,c+1):
if a%d==0 and b%d==0:
gcf = d
return(gcf)
elif a==0:
return(b)
elif b==0:
return(a)
i = int(input("enter a number"))
j = int(input("enter a number"))
g = f(i,j)
print("greatest common divisor =", g)

Q4. WAP to find the exponentiation of a number.


def expo(n,num):
return num**n

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


n = int(input("Enter the power to be raised"))

answer = expo(n,num)
print(answer)

Q5. WAP to find first n prime numbers.


def pm(n):
l = []
for i in range(n+1):
for j in range(i):
if i%(j+1) != 0:
[Link](i)
return l

n = int(input("Enter number:",))
primen = pm(n)
print("Prime number:",primen)

Q6. WAP to count the numbers of (i) characters, (ii) Vovels and
(iii) Consonents in the string. make saperate functions and call.
def chr(string):
count = 0
for i in string:
count = count + 1
return count
ch = input("enter the character:",)

n = chr(ch)
print(n)

def vowel(string):
count = 0
vowel = ['a','e','i','o','u','A','E','I','O','U']
for i in string:
if i in vowel:
count = count + 1
return count
ch = input("enter the character:",)

n = vowel(ch)
print(n)

def consonant(string):
count = 0
vowel = ['a','e','i','o','u','A','E','I','O','U']
for i in string:
if i not in vowel:
count = count + 1
return count
ch = input("enter the character:",)

n = consonant(ch)
print(n)

def l(lis):
t = lis
for i in lis:
[Link](i*2)
return t

p = []
a = int(input("Enter Number:",))
for i in range(a):
b = int(input("Enter the number to enter:",))
[Link](b)

n = l(p)
print(n)
Q7. Design a composite function
area(radius(decide_here_what_you_will_pass)) for calculating
the area
of a circle. Center of the circle is stored in (x_c, y_c) coordinates. A point on the parameter of a
circle is
given by (x_p, y_p). All the coordinates are taken as input from the user.

def radius(x_c,y_c,x_p,y_p):
redius = ((x_p-x_c)**2 + (y_p-y_c)**2)**(1/2)
return redius

def area(rad):
area_of_circle = 3.14*rad*rad
return area_of_circle

x_c = int(input("Enter x cordinate of point:",))


y_c = int(input("Enter y coordinate of point:",))
x_p = int(input("Enter x cordinate of center of circle:",))
y_p = int(input("Enter y coordinate of centre of circle:",))

answer = area(radius(x_c,y_c,x_p,y_p))
print("Areaa of circle is",answer)

Q8. Define a function to sum n natural numbers. Use sum and n


as a global variable (do not pass as an argument or local
variable) and print the value of sum in the main function.
sum = 0
n = int(input("enter the no. of natural numbers:",))
def sum_n():
global sum
for i in range(1,n+1):
sum = sum + i
print(sum)

sum_n()

Q9. Define a function in Python which takes a list as an


argument, multiplies every
element of the input list by 2 and append new elements to the
same list.
For example:
Input: [1,2,3]
Output: [1,2,3,2,4,6]
Show that the id of the list passed to the function is the same as the list processed within
the function.
ANS CODE:
def f(k):
b=list()
for i in range(k):
[Link](int(input("input list")))
n = len(b)
for j in range(n):
l=b
[Link](b[j]*2)
if id(l)==id(b):
print("both lists have the same id=", id(l))
return("old list",b ,"new list:", l)
k = int(input("enter a number"))
print(f(k))

Q10. Design a nested void function in Python (not-fruitful) to find


the reverse of a number
entered by a user. Address the following constraints:
1. Pass the number entered by the user as a positional argument
to the outer function.
2. reverse() is an inner function (nested function) and no
argument is passed to this
function.
3. Both the functions are void i.e., they do not return any value to
any function.
4. Do not use print statements with the functions.
ANS CODE:
def reverse_number(number):
reverse_num = 0

def reverse():
nonlocal reverse_num
n= number
while n > 0:
reverse_num = reverse_num*10 + n%10
n = n//10

reverse()
return reverse_num
num = int(input("enter a number"))
result = reverse_number(num)
print(f"The reversed number is: {result}")

You might also like