Answer to Question #273678 in C# for Holik

Question #273678

7. Describe a given integer n and convert it to binary, octal


Create a class with methods. Create an application that uses class elements.

1
Expert's answer
2021-11-30T18:41:06-0500
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();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS