asked 27.8k views
1 vote
In this chapter, you learned that although a double and a decimal both hold floating-point numbers, a double can hold a larger value. Write a C# program named DoubleDecimalTest that declares and displays two variables—a double and a decimal. Experiment by assigning the same constant value to each variable so that the assignment to the double is legal but the assignment to the decimal is not.

1 Answer

7 votes

Answer:

The DoubleDecimalTest class is as follows:

using System;

class DoubleDecimalTest {

static void Main() {

double doubleNum = 173737388856632566321737373676D;

decimal decimalNum = 173737388856632566321737373676M;

Console.WriteLine(doubleNum);

Console.WriteLine(decimalNum); } }

Step-by-step explanation:

Required

Program to test double and decimal variables in C#

This declares and initializes double variable doubleNum

double doubleNum = 173737388856632566321737373676D;

This declares and initializes double variable decimalNum (using the same value as doubleNum)

decimal decimalNum = 173737388856632566321737373676M;

This prints doubleNum

Console.WriteLine(doubleNum);

This prints decimalNum

Console.WriteLine(decimalNum);

Unless the decimal variable is commented out or the value is reduced to a reasonable range, the program will not compile without error.

answered
User Holi Boom
by
8.7k points