Write a C# Sharp program to determine the sign of a single value and display it to the console

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to determine the sign of a single value and display it to the console. 
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.

الأجوبة

using System;
using System.Text;
namespace exercises {
  class Program {
    public static void Main() {
      string str = "{0}: {1,3} is {2} zero.";
      string nl = Environment.NewLine;
      byte xByte1 = 0;
      short xShort1 = -2;
      int xInt1 = -3;
      long xLong1 = -4;
      float xSingle1 = 2.10f;
      double xDouble1 = 6.0;
      Decimal xDecimal1 = -7m;
      Console.WriteLine($"{nl}Test the sign of the following types of values:");
      Console.WriteLine(str, "Byte   ", xByte1, Test(Math.Sign(xByte1)));
      Console.WriteLine(str, "Int16  ", xShort1, Test(Math.Sign(xShort1)));
      Console.WriteLine(str, "Int32  ", xInt1, Test(Math.Sign(xInt1)));
      Console.WriteLine(str, "Int64  ", xLong1, Test(Math.Sign(xLong1)));
      Console.WriteLine(str, "Single ", xSingle1, Test(Math.Sign(xSingle1)));
      Console.WriteLine(str, "Double ", xDouble1, Test(Math.Sign(xDouble1)));
      Console.WriteLine(str, "Decimal", xDecimal1, Test(Math.Sign(xDecimal1)));
    }
    public static string Test(int compare) {
      if (compare == 0)
        return "equal to";
      else if (compare < 0)
        return "less than";
      else
        return "greater than";
    }
  }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...