# C# Sharp Math : Exercises, Practice, Solution

## C# Sharp Math Exercises [26 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 sideClick 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

**13.** Write a C# Sharp program to convert a given integer value to Roman numerals. Go to the editor

*Expected Output:*

Original integer value: 2365

Roman numerals of the said integer value:

MMCCCLXV

Original integer value: 254

Roman numerals of the said integer value:

CCLIV

Original integer value: 45

Roman numerals of the said integer value:

XLV

Original integer value: 8

Roman numerals of the said integer value:

VIII

Click me to see the solution

**14.** Write a C# Sharp program to calculate the largest integral value less than or equal and smallest integral value greater than or equal to a given number. Go to the editor

*Expected Output:*

Value largest int value smallest int value

Value less than or equal greater than or equal

8.03 9 8

8.34 9 8

0.12 1 0

-0.14 0 -1

-8.1 -8 -9

-8.6 -8 -9

Click me to see the solution

**15.** Write a C# Sharp program to convert a given Roman numerals value to an integer value. Go to the editor

*Expected Output:*

Original integer value: MMCCCLXV

Integer value of the said Roman numerals:

2365

Original integer value: CCLIV

Integer value of the said Roman numerals:

254

Original integer value: XLV

Integer value of the said Roman numerals:

45

Original integer value: VIII

Integer value of the said Roman numerals:

8

Click me to see the solution

**16.** Write a C# program to divide two given integers (dividend and divisor) and get the quotient without using multiplication, division and mod operator. Go to the editor

*Expected Output:*

Dividend = 7, Divisor = 3

Quotient= 2

Dividend = -17, Divisor = 5

Quotient= -3

Dividend = 35, Divisor = 7

Quotient= 5

Click me to see the solution

**17.** Write a C# program to given two non-negative integers n1 and n2 represented as string, return the string represented product of n1 and n2. Go to the editor

*Expected Output:*

Non-negative integer1(string) = 12, Non-negative integer2(string) = 5

Product = 60

Non-negative integer1(string) = 221, Non-negative integer2(string) = 415

Product = 91715

Non-negative integer1(string) = 0, Non-negative integer2(string) = 15

Product = 0

Click me to see the solution

**18.** Write a C# Sharp program to compute the sum of the positive and negative numbers of an array of integers and display the largest sum. Go to the editor

*Expected Output:*

Original array elements:

-10 -11 -12 -13 -14 15 16 17 18 19 20

Largest sum - Positive/Negative numbers of the said array: 105

Original array elements:

-11 -22 -44 0 3 4 5 9

Largest sum - Positive/Negative numbers of the said array: -77

Click me to see the solution

**19.** Write a C# Sharp program to find the value of PI up to n (given number) decimals places. Go to the editor

The number π (/paɪ/) is a mathematical constant. It is defined as the ratio of a circle's circumference to its diameter, and it also has various equivalent definitions. It appears in many formulas in all areas of mathematics and physics. The earliest known use of the Greek letter π to represent the ratio of a circle's circumference to its diameter was by Welsh mathematician William Jones in 1706. It is approximately equal to 3.14159. It has been represented by the Greek letter "π" since the mid-18th century, and is spelled out as "pi". It is also referred to as Archimedes' constant.

*Expected Output:*

Value of PI up to 2 decimals places -> 3.14

Value of PI up to 7 decimals places -> 3.1415927

Value of PI up to 15 decimals places -> 3.141592653589793

Click me to see the solution

**20.** Write a C# Sharp program to get the Least Common Multiple (LCM) of more than two numbers. Take the numbers from a given array of positive integers. Go to the editor

From Wikipedia,

In arithmetic and number theory, the least common multiple, lowest common multiple, or smallest common multiple of two integers a and b, usually denoted by lcm(a, b), is the smallest positive integer that is divisible by both a and b. Since division of integers by zero is undefined, this definition has meaning only if a and b are both different from zero. However, some authors define lcm(a,0) as 0 for all a, which is the result of taking the lcm to be the least upper bound in the lattice of divisibility.

*Expected Output:*

Original array elements:

4 6 8

LCM of the numbers of the said array of positive integers: 24

Original array elements:

1 2 3 4 5 6 7 8 9 10

LCM of the numbers of the said array of positive integers: 2520

Original array elements:

48 72 108

LCM of the numbers of the said array of positive integers: 432

Click me to see the solution

**21.** Write a C# Sharp program to get the nth tetrahedral number from a given integer(n) value. Go to the editor

A tetrahedral number, or triangular pyramidal number, is a figurate number that represents a pyramid with a triangular base and three sides, called a tetrahedron. The formula for the nth tetrahedral number is represented by the 3rd rising factorial of n divided by the factorial of 3:

Example of tetrahedral numbers:

N | Tetrahedral Number |
---|---|

1 | 1 |

2 | 4 |

3 | 10 |

4 | 20 |

5 | 35 |

6 | 56 |

*Expected Output:*

Original Number:1

Tetrahedral number:1

Original Number:2

Tetrahedral number:4

Original Number:6

Tetrahedral number:56

Click me to see the solution

**22.** Write a C# Sharp program to sort a given positive number in descending/ascending order. Go to the editor

Descending -> Highest to lowest.

Ascending -> Lowest to highest.

*Expected Output:*

Original Number: 134543

Descending order of the said number: 544331

Ascending order of the said number: 133445

Original Number: 4375973

Descending order of the said number: 9775433

Ascending order of the said number: 3345779

Click me to see the solution

**23.** Write a C# Sharp program to check whether a given number (integer) is Oddish or Evenish. Go to the editor

A number is called "Oddish" if the sum of all of its digits is odd, and a number is called "Evenish" if the sum of all of its digits is even.

Example:

120 -> Oddish

321 -> Evenish

43 -> Oddish

4433 -> Evenish

373 -> Oddish.

*Expected Output:*

Original number: 120

Check the said number is Oddish or Evenish! Oddish

Original number: 321

Check the said number is Oddish or Evenish! Evenish

Original number: 43

Check the said number is Oddish or Evenish! Oddish

Original number: 4433

Check the said number is Oddish or Evenish! Evenish

Click me to see the solution

**24.** Write a C# Sharp program to reverse the binary representation of an given integer and convert the reversed binary number into an integer. Go to the editor

*Expected Output:*

Original number: 120

Reverse the binary representation of the said integer and convert it into an integer: 15

Original number: 321

Reverse the binary representation of the said integer and convert it into an integer: 261

Original number: 43

Reverse the binary representation of the said integer and convert it into an integer: 53

Original number: 4433

Reverse the binary representation of the said integer and convert it into an integer: 4433

Click me to see the solution

**25.** Write a C# Sharp program to find the closest palindrome number of a given integer. If there are two palindrome numbers in absolute distance return the smaller number. Go to the editor

Click me to see the solution

**26.** Write a C# Sharp program that takes an integer check whether the number is uban or not. Go to the editor

**Sample Data:**

(63) -> True

(100) -> False

(100000) -> False

(1000005) -> True

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.**

**Exercises: Weekly Top 16 Most Popular Topics**- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation