﻿ C# Sharp: Math Exercises, Practice, Solution - w3resource # C# Sharp programming Exercises, Practice, Solution: Math

## C# Sharp Math Exercises [12 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a C# Sharp program to get the absolute value of a number of Decimal values, Double values, Int16 values, Int32 values: and Int64 values. Go to the editor
Expected Output:
Absolute value of a number of Decimal values:
Abs(79228162514264337593543950335) = 79228162514264337593543950335
Abs(15.55) = 15.55
Abs(0) = 0
Abs(-17.23) = 17.23
Abs(-79228162514264337593543950335) = 79228162514264337593543950335
Absolute value of a number of Double values:
Abs(1.79769313486232E+308) = 1.79769313486232E+308
Abs(1.5354E-16) = 1.5354E-16
Abs(14.098123) = 14.098123
Abs(0) = 0
Abs(-17.069713) = 17.069713
Abs(-1.4058E+19) = 1.4058E+19
Abs(-1.79769313486232E+308) = 1.79769313486232E+308
Absolute value of a number of Int16 values:
Abs(32767) = 32767
Abs(10328) = 10328
Abs(0) = 0
Abs(-1476) = 1476
Unable to calculate the absolute value of -32768.
Absolute value of a number of Int32 values:
Abs(2147483647) = 2147483647
Abs(16921) = 16921
Abs(0) = 0
Abs(-804128) = 804128
Unable to calculate the absolute value of -2147483648.
Absolute value of a number of Int64 values:
Abs(9223372036854775807) = 9223372036854775807 Abs(109013) = 109013
Abs(0) = 0
Abs(-6871982) = 6871982
Unable to calculate the absolute value of -9223372036854775808.
Click me to see the solution

2. Write a C# Sharp program to find the greater and smaller value of two variables. Go to the editor
Expected Output:
Display the greater of two values:
Byte : The greater of 1 and 51 is 51.
Int16 : The greater of -2 and 52 is 52.
Int32 : The greater of -3 and 53 is 53.
Int64 : The greater of -4 and 54 is 54.
Single : The greater of 5 and 55 is 55.
Double : The greater of 6 and 56 is 56.
Decimal: The greater of 7 and 57 is 57.
Display the lesser of two values:
Byte : The lesser of 1 and 51 is 1.
Int16 : The lesser of -2 and 52 is -2.
Int32 : The lesser of -3 and 53 is -3.
Int64 : The lesser of -4 and 54 is -4.
Single : The lesser of 5 and 55 is 5.
Double : The lesser of 6 and 56 is 6.
Decimal: The lesser of 7 and 57 is 7.
Click me to see the solution

3. Write a C# Sharp program to calculate the value that results from raising 3 to a power ranging from 0 to 32. Go to the editor
3^0 = 1 (0x1)
3^1 = 3 (0x3)
3^2 = 9 (0x9)
3^3 = 27 (0x1B)
3^4 = 81 (0x51)
3^5 = 243 (0xF3)
3^6 = 729 (0x2D9)
3^7 = 2,187 (0x88B)
3^8 = 6,561 (0x19A1)
3^9 = 19,683 (0x4CE3)
3^10 = 59,049 (0xE6A9)
3^11 = 177,147 (0x2B3FB)
3^12 = 531,441 (0x81BF1)
3^13 = 1,594,323 (0x1853D3)
3^14 = 4,782,969 (0x48FB79)
3^15 = 14,348,907 (0xDAF26B)
3^16 = 43,046,721 (0x290D741)
3^17 = 129,140,163 (0x7B285C3)
3^18 = 387,420,489 (0x17179149)
3^19 = 1,162,261,467 (0x4546B3DB)
3^20 = 3,486,784,401 (0xCFD41B91)
3^21 = 10,460,353,203 (0x26F7C52B3)
3^22 = 31,381,059,609 (0x74E74F819)
3^23 = 94,143,178,827 (0x15EB5EE84B)
3^24 = 282,429,536,481 (0x41C21CB8E1)
3^25 = 847,288,609,443 (0xC546562AA3)
3^26 = 2,541,865,828,329 (0x24FD3027FE9)
3^27 = 7,625,597,484,987 (0x6EF79077FBB)
3^28 = 22,876,792,454,961 (0x14CE6B167F31)
3^29 = 68,630,377,364,883 (0x3E6B41437D93)
3^30 = 205,891,132,094,649 (0xBB41C3CA78B9)
3^31 = 617,673,396,283,947 (0x231C54B5F6A2B)
3^32 = 1,853,020,188,851,841 (0x6954FE21E3E81)
Click me to see the solution

4. Write a C# Sharp program to calculate true mean value, mean with rounding away from zero and mean with rounding to nearest of some specified decimal values. Go to the editor
Expected Output:
True Mean: 16.36
Away From Zero: 16.37
Rounding to Nearest: 16.35
Click me to see the solution

5. Write a C# Sharp program to determine the sign of a single value and display it to the console. Go to the editor
Expected Output:
Test the sign of the following types of values:
Byte : 0 is equal to zero.
Int16 : -2 is less than zero.
Int32 : -3 is less than zero.
Int64 : -4 is less than zero.
Single : 2.1 is greater than zero.
Double : 6 is greater than zero.
Decimal: -7 is less than zero.
Click me to see the solution

6. Write a C# Sharp program to calculate the of each city's size in square from the given area of some cities in the United States. Go to the editor
Expected Output:

```City                   Area (mi.)   Equivalent to a square with:

Sitka, Alaska             2,870.3          53.58 miles per side
New York City               302.6          17.40 miles per side
Los Angeles                 468.7          21.65 miles per side
Detroit                     138.8          11.78 miles per side
Chicago                     227.1          15.07 miles per side
San Diego                   325.2          18.03 miles per side```
Click me to see the solution

7. Write a C# Sharp program to find the whole number and fractional part from a positive and a negative Decimal number, Double number. Go to the editor
Expected Output:
Original Decimal Number: 52.7365
The whole number and fractional part of the said positive Decimal number:
52
0.7365
Original Decimal Number: -52.736
The whole number and fractional part of the said negative Decimal number:
-52
-0.736
Original Double Number: 92.73165
The whole number and fractional part of the said positive Float number:
92
0.731650000000002
Original Double Number: -42.7636
The whole number and fractional part of the said negative Float number:
-42
-0.763599999999997
Click me to see the solution

8. Write a C# Sharp program to calculate the quotient of two 32-bit signed integers and also returns the remainder in an output parameter. Go to the editor
Expected Output:
2,147,483,647 \ 4,000 = 536,870, remainder 3,647
23,547 \ 4,000 = 5, remainder 3,547
0 \ 4,000 = 0, remainder 0
-12,547 \ 4,000 = -3, remainder -547
-2,147,483,648 \ 4,000 = -536,870, remainder -3,648
2,147,483,647 \ -4,000 = -536,870, remainder 3,647
23,547 \ -4,000 = -5, remainder 3,547
0 \ -4,000 = 0, remainder 0
-12,547 \ -4,000 = 3, remainder -547
-2,147,483,648 \ -4,000 = 536,870, remainder -3,648
Click me to see the solution

9. Write a C# Sharp program to calculate the full product of two 32-bit numbers. Go to the editor
Expected Output:
Calculate the product of two Int32 values:
2147483647 * 2147483647 = 4611686014132420609
Click me to see the solution

10. Write a C# Sharp program to reverse the digits of a given signed 32-bit integer. Go to the editor
Expected Output:
Original Integer value: 123456
Reverse the digits of the said signed integer value:
654321
Original Integer value: -7654
Reverse the digits of the said signed integer value:
-4567
Original Integer value: 100
Reverse the digits of the said signed integer value:
1
Click me to see the solution

11. Write a C# Sharp program to convert a given string value to a 32-bit signed integer. Go to the editor
Expected Output:
Original String value: 123456
Convert the said string to integer value:
123456
Original String value: +3456
Convert the said string to integer value:
3456
Original String value: -123456
Convert the said string to integer value:
-123456
Original String value: a1234
Convert the said string to integer value:
0
Original String value: 123a456
Convert the said string to integer value:
123
Click me to see the solution

12. Write a C# Sharp program to check whether a given integer is a palindrome integer or not. Return true if the number is palindrome otherwise return false. Go to the editor
Expected Output:
Original integer value: 123456
Check the said number is a palindrome number or not:
False
Original integer value: 16461
Check the said number is a palindrome number or not:
True
Original integer value: -121
Check the said number is a palindrome number or not:
False
Click me to see the solution

C# Sharp Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

﻿