/* 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); } }
Data Types
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 ); } }
decimal property
using System; class MainEntryPoint { static void Main(string[] args) { Money cash1 = new Money(); cash1.Amount = 40M; Console.WriteLine("cash1.ToString() returns: " + cash1.ToString()); Console.ReadLine(); } } class Money { private decimal amount; public decimal Amount { get { return amount; } set { amount = value; } } public override string ToString() { return "$" + Amount.ToString(); } }
Manually create a decimal number
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Manually create a decimal number. using System; public class CreateDec { public static void Main() { decimal d = new decimal(12345, 0, 0, false, 2); Console.WriteLine(d); } }
Compute the initial investment needed to attain a known future value given
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* Compute the initial investment needed to attain a known future value given annual rate of return and the time period in years. */ using System; public class IntialInvestment { public static void Main() { decimal InitInvest; // initial investment decimal FutVal; // future value double NumYears; // number of years double IntRate; // annual rate of return as a decimal string str; Console.Write("Enter future value: "); str = Console.ReadLine(); try { FutVal = Decimal.Parse(str); } catch(FormatException exc) { Console.WriteLine(exc.Message); return; } Console.Write("Enter interest rate (such as 0.085): "); str = Console.ReadLine(); try { IntRate = Double.Parse(str); } catch(FormatException exc) { Console.WriteLine(exc.Message); return; } Console.Write("Enter number of years: "); str = Console.ReadLine(); try { NumYears = Double.Parse(str); } catch(FormatException exc) { Console.WriteLine(exc.Message); return; } InitInvest = FutVal / (decimal) Math.Pow(IntRate+1.0, NumYears); Console.WriteLine("Initial investment required: {0:C}", InitInvest); } }