Visual C# 7.15 (Hypotenuse of a Right Triangle)

This Solution is for the “Hypotenuse of a Right Triangle” of chapter 6 in the textbook; Visual c# How to program 6th edition by Deitel.

//Date: 5/18/2017

/*Description:
Uses an expression bodied method to calculate the length of the hypotenuse of 3 right triangles
provided the lengths of the legs in the assignment. The method takes 2 double arguments and returns the hypotenuse as a double.
*/
using System;

class Program
{

public static void Main( string[] args )

{

double side1; // first side of triangle

double side2; // second side of triangle

 

Console.Write( “Enter side 1 (negative to quit): ” );

side1 = Convert.ToDouble( Console.ReadLine() );

 

while ( side1 > 0 )

{

Console.Write( “Enter side 2: ” );

side2 = Convert.ToDouble( Console.ReadLine() );

 

Console.WriteLine( “Hypotenuse is: {0}”,

Hypotenuse( side1, side2 ) );

 

Console.Write( “Enter side 1 (negative to quit): ” );

side1 = Convert.ToDouble( Console.ReadLine() );

} // end while

} // end Main

 

   // calculate hypotenuse given lengths of two sides

public static double Hypotenuse( double side1, double side2 )

{

double hypotenuseSquared = Math.Pow( side1, 2 ) +

Math.Pow( side2, 2 );

 

return Math.Sqrt( hypotenuseSquared );

} // end method Hypotenuse

Program Output:

Exercise 7.15
————-

The hypotenuse of Triangle 1 is: 5

The hypotenuse of Triangle 2 is: 13

The hypotenuse of Triangle 3 is: 17

Press any key to continue . . .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s