Question

In C Sharp Write a program that uses a recursive method to convert a number in...

In C Sharp

Write a program that uses a recursive method to convert a number in decimal to a given base b, where b is between 2 and 36.  Your program should prompt the user to enter the number in decimal and the desired base.  

Hint: The algorithm to convert a positive decimal number into an equivalent number in octal (or hexadecimal) starts by dividing the decimal number by 8 (for octal) and by 16 (for hexadecimal).  Suppose that abrepresents a to the base b. Then 7510represents 75 base 10 (decimal) and 7516represents 75 base 16 (hexadecimal).  Check out these equivalents:

75310= 13618

75310= 2F116

You can extend the idea of converting from a decimal to base 2, 8 or 16 to any base. Suppose you wish to convert a decimal number n into an equivalent number in base b, where b is between 2 and 36. You would start by dividing the number n by b.  Note: the digits in say base 20 are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I and J.

You need to write your program and test it using the test cases below.

Please answer In C Sharp

Test Data Inputs:

9098, base 20

692, base 2

753, base 16

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

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

namespace Conversion
{
class Program
{
static void Main(string[] args)
{
int inputNum , numBase ;
Console.Write("Enter Number To Convert: ");
inputNum = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Base: ");
numBase = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(inputNum +" In Base " + numBase + " Is " +fromDeci(numBase, inputNum));
Console.ReadKey();
}

// To return char for a value. For
// example '2' is returned for 2.
// 'A' is returned for 10. 'B' for 11
static char reVal(int num)
{
if (num >= 0 && num <= 9)
return (char)(num + 48);
else
return (char)(num - 10 + 65);
}

// Function to convert a given decimal number
// to a base 'base' and
static string fromDeci(int base1, int inputNum)
{
string s = "";

// Convert input number is given
// base by repeatedly dividing it
// by base and taking remainder
while (inputNum > 0)
{
s += reVal(inputNum % base1);
inputNum /= base1;
}
char[] res = s.ToCharArray();

// Reverse the result
Array.Reverse(res);
return new String(res);
}
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
In C Sharp Write a program that uses a recursive method to convert a number in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT