Mega Code Archive

 
Categories / C# Tutorial / Data Type
 

Using casts in an expression

using System;      class Example {        public static void Main() {          double n;           for(n = 1.0; n <= 10; n++) {          Console.WriteLine("The square root of {0} is {1}",                            n, Math.Sqrt(n));             Console.WriteLine("Whole number part: {0}" ,                             (int) Math.Sqrt(n));              Console.WriteLine("Fractional part: {0}",                             Math.Sqrt(n) - (int) Math.Sqrt(n) );          Console.WriteLine();      }     }      } The square root of 1 is 1 Whole number part: 1 Fractional part: 0 The square root of 2 is 1.4142135623731 Whole number part: 1 Fractional part: 0.414213562373095 The square root of 3 is 1.73205080756888 Whole number part: 1 Fractional part: 0.732050807568877 The square root of 4 is 2 Whole number part: 2 Fractional part: 0 The square root of 5 is 2.23606797749979 Whole number part: 2 Fractional part: 0.23606797749979 The square root of 6 is 2.44948974278318 Whole number part: 2 Fractional part: 0.449489742783178 The square root of 7 is 2.64575131106459 Whole number part: 2 Fractional part: 0.645751311064591 The square root of 8 is 2.82842712474619 Whole number part: 2 Fractional part: 0.82842712474619 The square root of 9 is 3 Whole number part: 3 Fractional part: 0 The square root of 10 is 3.16227766016838 Whole number part: 3 Fractional part: 0.16227766016838