Number conversion using constructor overloading in c#

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//program for number conversion using constructor overloading.


using System;

using System.Collections.Generic;

using System.Text;

namespace con_over

{

class overload

{

overload(int n)

{

cal(2,n);

}

overload(int n,int a)

{

cal(8, n);

}

overload(int n,int a, int b)

{

cal(16,n);

}

void cal(int ch,int n)

{

int[] rem=new int[50];

int i=0;

while (n != 0)

{

i++;

rem[i] = n % ch;

n = n / ch;

}

if (ch == 2)

Console.WriteLine(“BINARY EQUIVALENT:-“);

else if (ch ==8)

Console.WriteLine(“OCTAL EQUIVALENT:-“);

else if (ch == 16)

Console.WriteLine(“HEX EQUIVALENT:-“);

Console.Write(“(“);

for (int j = i; j > 0; j–)

{

if(ch==16)

{

if (rem[j] == 10)

Console.Write(“A “);

else if (rem[j] == 11)

Console.Write(“B “);

else if (rem[j] == 12)

Console.Write(“C “);

else if (rem[j] == 13)

Console.Write(“D “);

else if (rem[j] == 14) Console.Write(“E “);

else if (rem[j] == 15) Console.Write(“F “);

else

Console.Write(rem[j] + ” “);

}

else

Console.Write(rem[j] + ” “);

}

Console.Write(“)”+ch);

Console.WriteLine(” “);

}

static void Main(string[] args)

{

int num;

Console.WriteLine(“Enter any number”);

num = int.Parse(Console.ReadLine());

overload o = new overload(num);

overload p = new overload(num,1);

overload q = new overload(num,1,2);

Console.ReadLine();

}

}

}

/************OUTPUT***************

Enter any number

30

BINARY EQUIVALENT:-

(1 1 1 1 0 )2

OCTAL EQUIVALENT:-

(3 6 )8

HEX EQUIVALENT:-

(1 E )16

*/

Leave a Reply

Your email address will not be published. Required fields are marked *