w3resource

C#: Whole number and fractional part from a positive and a negative Decimal number, Double number

C# Sharp Math: Exercise-7 with Solution

Write a C# Sharp program to find the whole number and fractional part from a positive and negative Decimal number, Double number.

Sample Solution:

C# Sharp Code:

using System;
using System.Text;
namespace exercises {
  class Program {
    public static void Main() {
      decimal decimalNumber;
      decimalNumber = 52.7365m;
	  Console.WriteLine("Original Decimal Number: "+decimalNumber);
	  Console.WriteLine("The whole number and fractional part of the said positive Decimal number:");
      Console.WriteLine(Math.Truncate(decimalNumber));
	  Console.WriteLine(decimalNumber-Math.Truncate(decimalNumber));
      decimalNumber = -52.736m;
	  Console.WriteLine("Original Decimal Number: "+decimalNumber);
	  Console.WriteLine("The whole number and fractional part of the said negative Decimal number:");
	  Console.WriteLine(Math.Truncate(decimalNumber)); 
	  Console.WriteLine(decimalNumber-Math.Truncate(decimalNumber)); 
	  double floatNumber;
      floatNumber = 92.73165;
	  Console.WriteLine("Original Double Number: "+floatNumber);
	  Console.WriteLine("The whole number and fractional part of the said positive Float number:");
      Console.WriteLine(Math.Truncate(floatNumber));
	  Console.WriteLine(floatNumber-Math.Truncate(floatNumber));
      floatNumber = -42.7636;
	  Console.WriteLine("Original Double Number: "+floatNumber);
	  Console.WriteLine("The whole number and fractional part of the said negative Float number:");
	  Console.WriteLine(Math.Truncate(floatNumber)); 
	  Console.WriteLine(floatNumber-Math.Truncate(floatNumber));
	}
  }
}

Sample 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

Flowchart:

Flowchart: C# Sharp Exercises - Whole number and fractional part from a positive and a negative Decimal number, Double number.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: 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.
Next: Write a C# Sharp program to calculate the quotient of two 32-bit signed integers and also returns the remainder in an output parameter.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/math/csharp-math-exercise-7.php