Computer Laboratory 6
Computer Laboratory 6
Task1: Copy and paste the code below. Save (as triangle.cpp), compile and run it.
// Computing the area of a triangle
#include <iostream>
#include <cmath> using
namespace std;
// The function prototype
double TArea(double, double, double);
int main()
{
double a, b, c, area;
cout << "Enter the sides of the triangle: ";
cin >> a >> b >> c;
area = TArea(a, b, c);
cout << "The area of this triangle is " << area << endl;
return 0;
}
// Returns the area of any triangle
double TArea(double a, double b, double c)
{
// Check if any side is negative
if (a<0. || b<0. || c<0.) return 0.0;
// Check if any side is greater than sum of two
if (a >= b+c) return 0.0;
if (b >= a+c) return 0.0;
if (c >= a+b) return 0.0;
// Calculate and return the area
double u, area;
u = 0.5*(a+b+c);
area = sqrt(u*(u-a)*(u-b)*(u-c));
return area;
}
Task 2: Using recursion, write a func?on (named as fibo) to find nth Fibonacci number. Call this func?on in the
main program with the argument n.
Fibonacci Series:
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th ...
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...
Task 4: