Write C# code to 1.Convert binary to decimal 2.Convert decimal to hexadecimal 3.Convert decimal to binary 4.Convert decimal to octal


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Conversion
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Enter your Choice : ");
                Console.WriteLine("1. Binary to Decimal");
                Console.WriteLine("2. Decimal to Hexadecimal");
                Console.WriteLine("3. Decimal to Binary");
                Console.WriteLine("4. Decimal to octal");
                Console.WriteLine("5. All");
                Console.WriteLine("6. Exit");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        B2D();
                        break;
                    case 2:
                        D2H();
                        break;
                    case 3:
                        D2B();
                        break;
                    case 4:
                        D2O();
                        break;
                    case 5:
                        B2D();
                        D2H();
                        D2B();
                        D2O();
                        break;
                    case 6:
                        break;

                    default:
                        Console.WriteLine("Enter valid choice");
                        break;
                } if (choice == 6)
                    break;
            }
            Console.ReadKey();
        }

        static void B2D()
        {
            Console.WriteLine("Enter a binary number:");
            String binary = Console.ReadLine();
            char[] b = binary.ToCharArray();
            int i = 0;
            int[] r = new int[20];
            r[0] = 1;
            int t = 1;
            for (i = 1; i < 20; i++)
            {
                t = t * 2;
                r[i] = t;
            }
            int d = 0, j = 0;
            for (i = b.Length - 1; i >= 0; i--)
            {
                if (b[i] == '1')
                {
                    d = d + r[j];
                }
                j++;
            }
            Console.WriteLine("Decimal number:\t" + d);
        }

        static void D2H()
        {
            Console.WriteLine("Enter a Decimal Number:");
            int d = int.Parse(Console.ReadLine());
            char[] t = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
            int i = 0;
            char[] h = new char[10];
            while (d > 0)
            {
                int temp = d % 16;
                h[i] = t[temp];
                d = d / 16;
                i++;
            }
            Console.Write("Hexadecimal Number:\t");
            i--;
            for (; i >= 0; i--)
            {
                Console.Write(h[i]);
            }
            Console.Write("\n");
        }

        static void D2B()
        {
            Console.WriteLine("Enter a decimal number:");
            int d = int.Parse(Console.ReadLine());
            int[] b = new int[15];
            int i = 0;
            while (d > 0)
            {
                int temp = d % 2;
                b[i] = temp;
                d = d / 2;
                i++;
            }
            i--;
            Console.Write("Binary Number:\t");
            for (; i >= 0; i--)
            {
                Console.Write(b[i]);
            }
            Console.Write("\n");
        }

        static void D2O()
        {
            Console.WriteLine("Enter a Decimal Number:");
            int d = int.Parse(Console.ReadLine());
            char[] t = { '0', '1', '2', '3', '4', '5', '6', '7' };
            int i = 0;
            char[] h = new char[10];
            while (d > 0)
            {
                int temp = d % 8;
                h[i] = t[temp];
                d = d / 8;
                i++;
            }
            Console.Write("Octal Number:\t");
            i--;
            for (; i >= 0; i--)
            {
                Console.Write(h[i]);
            }
            Console.Write("\n");

        }
    }
}

Output:
Enter your Choice :
1. Binary to Decimal
2. Decimal to Hexadecimal
3. Decimal to Binary
4. Decimal to octal
5. All
6. Exit

5
Enter a binary number:
101
Decimal number: 5

Enter a Decimal Number:
699
Hexadecimal Number:     2BB

Enter a decimal number:
7
Binary Number:  111

Enter a Decimal Number:
9
Octal Number:   11

Popular posts from this blog

Write a program to check whether empty query string is entered in Asp.Net.

Mandavrayji Temple, Muli, Surendranagar, Gujarat

Write a program to change color of Label text control programmatically in Asp.Net.