This C# program demonstrates how to use some of the fundamental mathematics functions and constants in C#.
using System;
namespace XoaX {
class Program {
static void Main(string[] args) {
int iA = 287;
int iD = 31;
int iQ = 0;
int iR = Math.DivRem(iA, iD, out iQ);
Console.WriteLine("The division algorithm:");
Console.WriteLine(iA + " = " + iQ + "*" + iD + " + " + iR);
Console.WriteLine();
int iLarge1 = 0x7fffffff;
int iLarge2 = 0x7fffffff;
long lProduct = Math.BigMul(iLarge1, iLarge2);
Console.WriteLine("Large multiplication with no overflow:");
Console.WriteLine(iLarge1 + " * " + iLarge2 + " = " + lProduct);
Console.WriteLine();
double dX = 7.0;
Console.WriteLine("Logarithms:");
Console.WriteLine("The natural log of " + dX + " is " + Math.Log(dX));
Console.WriteLine("The log base 2 of " + dX + " is " + Math.Log(dX, 2.0));
Console.WriteLine("The log base 10 of " + dX + " is " + Math.Log10(dX));
Console.WriteLine();
double dA = 37.2;
double dD = 5.7;
double dR = Math.IEEERemainder(dA, dD);
Console.WriteLine("Remainder:");
Console.WriteLine("The remainder of " + dA + " and " + dD + " is " + dR);
Console.WriteLine();
double dB = 2.5;
double dPow = 4.3;
Console.WriteLine("The exponential and power functions:");
Console.WriteLine("The exponential of " + dB + " is " + Math.Exp(dB));
Console.WriteLine(dB + " to the power " + dPow + " is " + Math.Pow(dB, dPow));
Console.WriteLine();
double dY = 8.1;
Console.WriteLine("The sqrt function:");
Console.WriteLine("The square root of " + dY + " is " + Math.Sqrt(dY));
Console.WriteLine();
Console.WriteLine("Constants");
Console.WriteLine("The value of pi is " + Math.PI);
Console.WriteLine("The value of e is " + Math.E);
}
}
}
The division algorithm: 287 = 8*31 + 9 Large multiplication with no overflow: 2147483647 * 2147483647 = 4611686014132420609 Logarithms: The natural log of 7 is 1.94591014905531 The log base 2 of 7 is 2.8073549220576 The log base 10 of 7 is 0.845098040014257 Remainder: The remainder of 37.2 and 5.7 is -2.7 The exponential and power functions: The exponential of 2.5 is 12.1824939607035 2.5 to the power 4.3 is 51.4211798568061 The sqrt function: The square root of 8.1 is 2.84604989415154 Constants The value of pi is 3.14159265358979 The value of e is 2.71828182845905 Press any key to continue . . .
© 20072025 XoaX.net LLC. All rights reserved.