using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("-> double.Epsilon: {0}", double.Epsilon); Console.WriteLine("-> double.PositiveInfinity: {0}", double.PositiveInfinity); Console.WriteLine("-> double.NegativeInfinity: {0}", double.NegativeInfinity); Console.WriteLine("-> double.MaxValue: {0}", double.MaxValue); Console.WriteLine("-> double.MinValue: {0}", double.MinValue); } }
converts Fahrenheit to Celsius
/* This program converts Fahrenheit to Celsius. Call this program FtoC.cs. */ using System; public class FtoC { public static void Main() { double f; // holds the temperature in Fahrenheit double c; // holds the temparture in Celsius f = 59.0; // start with 59 degrees Fahrenheit c = 5.0 / 9.0 * (f - 32.0); // convert to Celsius Console.Write(f + " degrees Fahrenheit is "); Console.WriteLine(c + " degrees Celsius."); } }
Talking to Mars: double value calculation
/* C# A Beginner's Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295 */ /* Project 2-1 Talking to Mars Call this file mars.cs */ using System; public class Mars { public static void Main() { double distance; double lightspeed; double delay; double delay_in_min; distance = 34000000; // 34,000,000 miles lightspeed = 186000; // 186,000 per second delay = distance / lightspeed; Console.WriteLine("Time delay when talking to Mars: " + delay + " seconds."); delay_in_min = delay / 60; Console.WriteLine("This is " + delay_in_min + " minutes."); } }
Implement the Pythagorean Theorem
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Implement the Pythagorean Theorem. using System; public class Pythagorean { public static void Main() { double s1; double s2; double hypot; string str; Console.WriteLine("Enter length of first side: "); str = Console.ReadLine(); s1 = Double.Parse(str); Console.WriteLine("Enter length of second side: "); str = Console.ReadLine(); s2 = Double.Parse(str); hypot = Math.Sqrt(s1*s1 + s2*s2); Console.WriteLine("Hypotenuse is " + hypot); } }
the differences between int and double
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* This program illustrates the differences between int and double. */ using System; public class Example3IntDouble { public static void Main() { int ivar; // this declares an int variable double dvar; // this declares a floating-point variable ivar = 100; // assign ivar the value 100 dvar = 100.0; // assign dvar the value 100.0 Console.WriteLine("Original value of ivar: " + ivar); Console.WriteLine("Original value of dvar: " + dvar); Console.WriteLine(); // print a blank line // now, divide both by 3 ivar = ivar / 3; dvar = dvar / 3.0; Console.WriteLine("ivar after division: " + ivar); Console.WriteLine("dvar after division: " + dvar); } }
Compute the area of a circle
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Compute the area of a circle. using System; public class ComputeCircle { static void Main() { double radius; double area; radius = 10.0; area = radius * radius * 3.1416; Console.WriteLine("Area is " + area); } }
explicit conversions from decimal to int
using System; class DecimalToU_Int32Demo { const string formatter = "{0,17}{1,19}{2,19}"; public static string GetExceptionType( Exception ex ) { string exceptionType = ex.GetType( ).ToString( ); return exceptionType.Substring(exceptionType.LastIndexOf( '.' ) + 1 ); } public static void DecimalToU_Int32( decimal argument ) { object Int32Value; object UInt32Value; try { Int32Value = (int)argument; } catch( Exception ex ) { Int32Value = GetExceptionType( ex ); } try { UInt32Value = (uint)argument; } catch( Exception ex ) { UInt32Value = GetExceptionType( ex ); } Console.WriteLine( formatter, argument, Int32Value, UInt32Value ); } public static void Main( ) { DecimalToU_Int32( 123M ); } }