Here are PRIP 4.2 Sumita Arora Solutions for class 12 Computer Science. To view chapter 4 conceptual videos, visit here.

To view Sumita Arora solutions for all chapters, visit here.

## Q.1: Area of an equilateral triangle can be computed as (√3/4) a^{2}, where a is its side. Write a program to obtain side from user and print the area of equilateral triangle with side a.

**Answer:**

Code:

import math # import math module

def areaOfEquilateral(a): #define function to calculate area

area = (math.sqrt(3)/4)*(a**2) # formula implementation

return area # returns area

side = float(input("Enter side of Equilateral Triangle : "))

area = areaOfEquilateral(side)

print("Area of Equilateral triangle of side {} is {}.".format(side,area))

Output:

Enter side of Equilateral Triangle : 10

Area of Equilateral triangle of side 10.0 is 43.30127018922193.

## Q.2: Area of regular polygon can be computed as:(1/2) n sin (360^{o}/n) S^{2}

Where,

n = number of sides

S = length from centre to a corner

Write a program to obtain values n and S from user and print area of polygon.

**Answer:**

Code:

from math import sin, pi

def areaOfPolygon(n,S): # define function to calculate area

area = (1/2)*n*sin((360/n) * pi/180)*(S**2) # formula implementation

return area # return area

no_of_side = float(input("Enter number of side of Polygon: "))

length = float(input("Enter length from centre to a corner: "))

area = areaOfPolygon(no_of_side, length)

print("Area of regular polygon is {}.".format(area))

Output:

Enter number of side of Polygon: 6

Enter length from centre to a corner: 9

Area of regular polygon is 210.4441731196186.

## Q.3:

(a) In Python shell, type the following

import math

Now type the following statements one by one and log that happens.

(b) Now quit the Python shell. Again restart Python IDLE shell and type

from math import *

Type the same set of statements given in part (a) i.e., (i) to (iv). Log what happens.

**(a) ****In Python shell, type the following****import math****Now type the following statements one by one and log that happens.**

**(i) math.sqrt(49) ****Answer:** 7.0**(ii) sqrt(49) ****Answer: **Error: NameError: name ‘sqrt’ is not defined

**(iii) math.fabs(-3)****Answer:** 3.0**(iv) fabs(-3)****Answer:** Error: NameError: name ‘fabs’ is not defined**(b) Now quit the Python shell. Again restart Python IDLE shell and type****from math import *****Type the same set of statements given in part (a) i.e., (i) to (iv). Log what happens.****(i) math.sqrt(49) ****Answer:** Error: NameError: name ‘math’ is not defined**(ii) sqrt(49) ****Answer:** 7.0

**(iii) math.fabs(-3)****Answer:** Error: NameError: name ‘math’ is not defined**(iv) fabs(-3)****Answer:** 3.0

## 4. Write a program to randomly generate a number from the range `100-1000`

both inclusive and print the generated number as

`randomly selected lucky viewer is : <number>`

**Answer:**

Code:

import random

def random_viewer():

return random.randint(100,1000)

print(" randomly selected lucky viewer is: {}".format(random_viewer()))Output:

randomly selected lucky viewer is: 555

Clear Doubts with Computer Tutor

In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to joinCTs learner-teacher community:students.computertutor.in