Está en la página 1de 2

Deadline for submission

EL125 – Programming Fundamentals Monday April 6, 2020


Assignment questions - I

Program to find the area of a triangle


There are various approaches to calculate the area of a triangle. We are
discussing two of those approaches.

Approach 1: Knowing base and height


When we know the base and height of the triangle, it is easy. It is simply half of
‘b’ (base) times the ‘h’ (height):
1
𝑎𝑟𝑒𝑎 = 2 𝑏. ℎ

Suggested Algorithm:
1. Take input of base and height of the triangle (use appropriate data types) Triangle area
2. Calculate the area as per the formula calculation
3. Display the area of the triangle

Program to be implemented
Write a C++ program that takes the base and height of the triangle as input and display its area. (Use
appropriate data types)

Approach 2: Knowing three sides of triangle (The Heron’s formula)


You can calculate the area of a triangle if you know the lengths of all
three sides, using a formula called "Heron's Formula".
Just use this two-step process:

Step 1: Calculate ‘s’ (half of the triangles perimeter):


(𝑎 + 𝑏 + 𝑐)
𝑠=
2
Where a, b and c are the sides of the triangle.
Step 2: Then calculate the Area:
𝑎𝑟𝑒𝑎 = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐) Triangle sides a, b and c.

Example:
What is the area of a triangle where every side is 5 units long?
(5+5+5)
Step1: 𝑠 = 2
= 7.5
Step2: 𝑎𝑟𝑒𝑎 = √7.5 × 2.5 × 2.5 × 2.5 = 10.825 …

Suggested Algorithm:
1. Take input of the three sides of the triangle (use appropriate data types)
2. Calculate half of the triangle’s perimeter ‘s’ (as in step 1)
3. Calculate the area of the triangle (as in step 2)
4. Display the area of the triangle

Program to be implemented
Write a C++ program that takes the three sides of the triangle as input and display its area using Heron’s
formula. (Use appropriate data types)

Challenge your brain!


Is there any other way to calculate the area of a triangle? If yes, make a C++ program to show
how it works.

Page 1 of 2
Program to calculate the salary of the employee
Given the following constrains and we have to calculate net salary of an employee.

1. Allowances:
i. Education Allowance (EA): 12% of Basic salary
ii. House Rent Allowance (HRA): 25% of Basic salary
iii. Medical Allowance (MA): 15% of Basic salary
iv. Travelling Allowance (TA): 10% of Basic salary
2. Tax cuts:
i. Provident Fund (PF) :10% of Basic salary, and
ii. Income Tax (IT): 15% of Basic salary

Net Salary = Basic Salary + EA + MA+ HRA + TA – (PF + IT)

Program to be implemented
Write a program using C++ that takes Basic Salary of an employee as input and display the net salary as
well as all the allowances and tax cuts. (Use appropriate data types)

Program to swap values of two integers variables


To write swap two numbers program in C++ is very simple and easy. You just need 3 variables and =
operator. Swap numbers means exchange the values of two variables with each other. For example,
variable num1 contains 200 and num2 contains 400. After swapping, variable num1 would contain 400
and num2 would contain 200.

Program to be implemented
Write a C++ program that takes two integer values, as input, in two variables and perform the swapping
of values in these variables.

Finding circumference and area of circle


If radius ‘r’ of a circle is given, we can easily calculate the circumference and area of the circle using the
following formulae:
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2

Program to be implemented
Write a C++ program that takes radius (floating point) of a circle as input and display the circumference
and area of the circle.

Challenge your brain!


In addition to display the circumference and area; can we extend the above program to calculate
and display the volume of the sphere as well? If yes, make a C++ program to show how it works.

Note for the students:


1. Add the following information on the top of your C++ source program as comments:
i. Name, roll number and section.
ii. Question to be solved.
2. Paste all of the C++ code in a word document and submit it before the deadline.
3. Try to solve the problem by yourself. It will help you developing your programming skills.

Page 2 of 2

También podría gustarte