C# Data Types Calculator & Pythagoras

This comprehensive guide presents three essential C# programming tasks covering data type handling, arithmetic operations, bitwise and logical operators, and mathematical calculations. The first program demonstrates the storage capabilities and limitations of float, double, and decimal data types with various numeric values. The second task creates a complete calculator that performs arithmetic, comparison, bitwise, and logical operations on user inputs. The third program implements the Pythagoras theorem to calculate the hypotenuse of a right-angled triangle. These exercises provide hands-on experience with C#'s numeric types, operators, and mathematical functions, making them ideal for beginners understanding data representation and computational logic.
Task 1: Data Types - Float, Double & Decimal Storage
This program demonstrates which values can be stored in float, double, and decimal data types, showing precision differences and rounding effects.
Source Code:
decimal g = 3456.091124875956542151256683467M; double a = 5, b = -5.01, c = 34.567839023, d = 12.345, e = 8923.1234857, f = 3456.091124875956542151256683467; Console.WriteLine("Value of a = " + a + "\t\t\t\t\t(It is Stored in Double)"); Console.WriteLine("Value of b = " + b + "\t\t\t\t(It is Stored in Double)"); Console.WriteLine("Value of c = " + c + "\t\t\t(It is Stored in Double)"); Console.WriteLine("Value of d = " + d + "\t\t\t\t(It is Stored in Double)"); Console.WriteLine("Value of e = " + e + "\t\t\t(It is Stored in Double)"); Console.WriteLine("Value of f = " + f + "\t\t\t(It is Stored in Double But its Round off value)"); Console.WriteLine("If We Store Value of f in Decimal "); Console.WriteLine("Value of f = " + g + "\t(Its Stored in Decimal But its again a Round off value");
Task 2: Simple Calculator - Arithmetic, Bitwise & Logical Operations
This program creates a comprehensive calculator that performs arithmetic operations, comparison operators, bitwise operations, and logical operations on two numbers.
Source Code:
int num1, num2; bool x, y; Console.WriteLine("Enter First Number : "); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Second Number : "); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("******************ARITHMETIC******************\n"); Console.WriteLine("{0} + {1} = {2}", num1, num2, num1 + num2); Console.WriteLine("{0} + {1} = {2}", num1, num2, num1 + num2++); Console.WriteLine("{0} - {1} = {2}", num1, num2, num1 - num2--); Console.WriteLine("{0} - {1} = {2}", num1, num2, num1 - num2); Console.WriteLine("{0} * {1} = {2}", num1, num2, num1 * num2); Console.WriteLine("{0} / {1} = {2}", num1, num2, num1 / num2); Console.WriteLine("{0} % {1} = {2}\n", num1, num2, num1 % num2); Console.WriteLine("******************COMPARISON******************\n"); Console.WriteLine("{0} > {1} = {2}", num1, num2, num1 > num2); Console.WriteLine("{0} < {1} = {2}", num1, num2, num1 < num2); Console.WriteLine("{0} >= {1} = {2}", num1, num2, num1 >= num2); Console.WriteLine("{0} <= {1} = {2}", num1, num2, num1 <= num2); Console.WriteLine("{0} == {1} = {2}", num1, num2, num1 == num2); Console.WriteLine("{0} != {1} = {2}", num1, num2, num1 != num2); Console.WriteLine("{0} & {1} = {2}", num1, num2, num1 & num2); Console.WriteLine("{0} | {1} = {2}", num1, num2, num1 | num2); Console.WriteLine("{0}^2 = {1}", num2, num2 ^ 2); Console.WriteLine("{0} << {1} = {2}", num1, num2, num1 << num2); Console.WriteLine("{0} >> {1} = {2}", num1, num2, num1 >> num2); Console.WriteLine("{0} != {1}", num2, num2!); Console.WriteLine("________________________________________\n"); Console.WriteLine("Enter First Value : "); x = Convert.ToBoolean(Console.ReadLine()); Console.WriteLine("Enter Second Value : "); y = Convert.ToBoolean(Console.ReadLine()); Console.WriteLine("*******************LOGICAL*******************\n"); Console.WriteLine("{0} && {1}= {0}", x, y, x && y); Console.WriteLine("{0} || {1}= {0}", x, y, x || y); Console.WriteLine("!{0}= {0}", y, !y); Console.WriteLine("{0} ^ {1}= {0}", x, y, x ^ y); Console.WriteLine("________________________________________");
Task 3: Hypotenuse Calculator - Pythagoras Theorem
This program calculates the hypotenuse of a right-angled triangle using the Pythagoras theorem: c² = a² + b².
Source Code:
double a, b, a2, b2, c2, ptheorem; Console.Write("Enter Value of A : "); a = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter Value of B : "); b = Convert.ToDouble(Console.ReadLine()); a2 = Convert.ToDouble(Math.Pow(a, 2)); b2 = Convert.ToDouble(Math.Pow(b, 2)); c2 = a2 + b2; Console.WriteLine("\nc^2 = a^2 + b^2"); Console.WriteLine("\nc^2 = ({0:0.00} + {1:0.00})", a2, b2); Console.WriteLine("\nc^2 = {0:0.00}", c2); ptheorem = Math.Sqrt(c2); Console.WriteLine("\nc = {0:0.00}", ptheorem);
CodingsHub2
Contributor at Jorvea — Free Guest Blogging & Content Publishing Platform
Frequently Asked Questions
What is the difference between logical (&&, ||) and bitwise (&, |) operators?
Logical operators (&&, ||) work with boolean values and perform short-circuit evaluation where the second operand is only evaluated if necessary, while bitwise operators (&, |) work on integer types bit-by-bit and always evaluate both operands. Logical AND (&&) returns true only if both operands are true, while bitwise AND (&) performs bitwise comparison on integer values. Logical operators are used for conditional logic, whereas bitwise operators are used for low-level bit manipulation.
How does the Math.Pow() method work in C#?
Math.Pow() is a static method in the System namespace that raises a specified number to the specified power. It takes two double parameters (base and exponent) and returns a double result. For example, Math.Pow(5, 2) calculates 5² = 25. The method handles various edge cases including negative bases, fractional exponents, and special values like infinity. It's commonly used in scientific calculations, geometric computations, and when implementing mathematical formulas in C# programs.
What is the Math.Sqrt() method and how is it used in Pythagoras theorem?
Math.Sqrt() is a static method that calculates the square root of a specified number. In the Pythagoras theorem calculation, Math.Sqrt(c2) computes the hypotenuse by taking the square root of the sum of squares (c²). The method returns the positive square root of the input value and throws an exception if the input is negative. It's essential for geometric calculations, distance computations, and implementing mathematical formulas that require square root operations in C# applications.
Why does converting large decimal numbers to double cause rounding?
Double uses binary floating-point representation with 53 bits of mantissa precision (approximately 15-16 decimal digits), which cannot precisely represent many decimal numbers. When storing a decimal value like 3456.091124875956542151256683467, the binary representation approximates the value, resulting in rounding. Decimal type uses base-10 representation with 28-29 significant digits, providing more accurate decimal representation but with performance trade-offs. This is why financial applications typically use decimal while scientific applications use double for performance.




