# Learn 2 CSharp 4 and 5

4. Reynolds number is calculated using formula (D*v*rho)/mu Where D = Diameter, V= velocity, rho = density mu = viscosity Write a program that will accept all values in appropriate units (Don't worry about unit conversion) If number is < 2100, display Laminar flow, If it's between 2100 and 4000 display 'Transient flow' and if more than '4000', display 'Turbulent Flow' (If, else, then...)

5. Modify the above program such that it will ask for 'Do you want to calculate again (y/n), if you say 'y', it'll again ask the parameters. If 'n', it'll exit. (Do while loop)

While running the program give value mu = 0. See what happens. Does it give 'DIVIDE BY ZERO' error? Does it give 'Segmentation fault..core dump?'. How to handle this situation. Is there something built in the language itself? (Exception Handling)

```using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lrn2csharp4_5 { class Program { static void Main( string[ ] args ) { char answer = 'y'; Console.WriteLine( "Today we are going to calculate Reynolds Number (D*v*rho)/mu" ); while ( answer.Equals( 'y' ) ) { try { calcReynolds( ); } catch ( System.DivideByZeroException dbze ) { Console.WriteLine( dbze.Message ); } Console.Write( "Do you want to calculate again (y/n): " ); try { answer = Convert.ToChar( Console.ReadLine( ) ); } catch ( System.FormatException ) { answer = 'n'; } } } static void calcReynolds( ) { Console.Write( "Enter Diameter: " ); double d = Convert.ToDouble( Console.ReadLine( ) ); Console.Write( "Enter Velocity: " ); double v = Convert.ToDouble( Console.ReadLine( ) ); Console.Write( "Enter Density: " ); double rho = Convert.ToDouble( Console.ReadLine( ) ); Console.Write( "Enter Viscosity: " ); double mu = Convert.ToDouble( Console.ReadLine( ) ); // C# actually handles divide by zero well and wont throw an exception automatically, so do it manually if ( mu.Equals( 0 ) ) throw new System.DivideByZeroException( "Cannot divide by zero. Viscosity was zero. You erred." ); double reynolds = ( d * v * rho ) / mu; if ( reynolds < 2100 ) Console.WriteLine( "Laminar flow (" + reynolds + ")" ); else if ( reynolds >= 2100 && reynolds <= 4000 ) Console.WriteLine( "Transient flow (" + reynolds + ")" ); else Console.WriteLine( "Turbulent flow (" + reynolds + ")" ); } } } ```

posted by dharh 3:01 AM Nov 29th, 2009