Define a function called hypotenuse that calculates the leng

Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. The function should take two arguments of type double and return the hypotenuse as a double. Test your program with the side values specified as follows: Write a function integer Power (base, exponent) that returns the value of base^exponent For example. integorPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer, and base is an integer. Function integer Power should use for to control the calculation. Do not use any math library functions. Write the int main(void) function as a driver program and call the above two functions result with sample Input/Output.

Solution

C code:

#include <stdio.h>
#include <math.h>
double hypotenuse(   double s1, double s2)
{
   double ans = sqrt(s1*s1 + s2*s2);
   return ans;
}

int integerPower(int base, int exp)
{
int m = 1;
int i =0;
while(i<exp)
    {
        i++;
    m = m*base;
}
return m;
}

int main()
{
   double s1,s2;
   s1 = 3; s2 = 4;
   double h = hypotenuse(s1,s2);  
   printf(\"%s%f\ \",\"s1 = 3, s2 = 4, hypotenuse = \", h);

   s1 = 5; s2 = 12;
   h = hypotenuse(s1,s2);  
   printf(\"%s%f\ \",\"s1 = 5, s2 = 12, hypotenuse = \", h);

   s1 = 8; s2 = 15;
   h = hypotenuse(s1,s2);  
   printf(\"%s%f\ \",\"s1 = 8, s2 = 15, hypotenuse = \", h);

int base = 3;int power = 4;
int out = integerPower(base,power);
printf(\"%i%s%d%s%d\ \",base,\" to power \",power, \" = \", out);
   return 0;
}

Sample Output:

s1 = 3, s2 = 4, hypotenuse = 5.000000
s1 = 5, s2 = 12, hypotenuse = 13.000000
s1 = 8, s2 = 15, hypotenuse = 17.000000
3 to power 4 = 81

 Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. The function should ta

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site