In: Computer Science
I. Design the C++ code for a program which will use the Pythagorean Theroem to calculate and display the length of a right triangle hypotenuse,c, given the lengths of its adjacent sides: a and b.. (hint: a^2 + b^2 = c^2)
II. Design the C++ code for a program which will calculate and display the roots of a quadratic equation of the form ax^2 + bx + c = 0, given its coefficients, a, b, and c.
1. C++ program for calculating the length of the hypotenuse of a right angled triangle:-
Formula used: - c = sqrt(a^2 + b^2)
#include <iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
double a,b,c;
printf("Enter the value of side A:-
\n");
scanf("%lf",&a);
printf("Enter the value of sideB:-
\n");
scanf("%lf",&b);
c = sqrt(a*a + b*b);
printf("The length of the Hypotenuse of the right
angled triangle is:- %lf",c);
return 0;
}
Output:-
Enter the value of side A:- 3
Enter the value of sideB:- 4
The length of the Hypotenuse of the right angled triangle is:-
5
2. C++ program to calculate the roots of the quadratic equation :-
Formula Used :- x = (-b + sqrt(b^2 - 4*a*c))/2*a y = (-b - sqrt(b^2 - 4*a*c))/2*a
#include <iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
double a,b,c;
double x,y;
printf("Enter the value of coefficient a:-
\n");
scanf("%lf",&a);
printf("Enter the value of coefficient b:-
\n");
scanf("%lf",&b);
printf("Enter the value of coefficient c:-
\n");
scanf("%lf",&c);
x = (-b + sqrt(b*b - 4*a*c))/(2*a);
y = (-b - sqrt(b*b - 4*a*c))/(2*a);
printf("The values of the roots x and y of the
quadratic equation are:- %lf and %lf",x,y);
return 0;
}
Output:-
Enter the value of coefficient a:- 4
Enter the value of coefficient b:- 4
Enter the value of coefficient c:- 1
The values of the roots x and y of the quadratic equation are:-
-0.500000 and -0.500000