7. Describe a given integer n and convert it to binary, octal
Create a class with methods. Create an application that uses class elements.
using System;
namespace ConsoleAppConvertToBinaryAndOctal
{
public class Number
{
int Value { get; set; }
public Number(int value)
{
Value = value;
}
public Number() { }
public string GetBinary()
{
return Convert.ToString(Value, 2);
}
public string GetOctal()
{
return Convert.ToString(Value, 8);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter a positive number:");
int num;
if (int.TryParse(Console.ReadLine(), out num))
{
if (num < 0)
{
Console.WriteLine("The entered number must be positive.");
}
else
{
Number number = new Number(num);
Console.WriteLine($"Binary representation of a number: {number.GetBinary()}.");
Console.WriteLine($"Octal representation of a number: {number.GetOctal()}.");
}
}
else
{
Console.WriteLine("The entered value cannot be converted to a number.");
}
Console.ReadKey();
}
}
}
Comments
Leave a comment