Base conversion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Base conversion

please help with a program that converts any base to base 10 by accepting input from the user which is the base and the number they wish to convert. -1 should signal the end if the user's input

20th Mar 2017, 11:14 PM
Thabiso Mcd Mohlomi
Thabiso Mcd Mohlomi - avatar
4 Answers
+ 2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Console.WriteLine("What is the number you want to convert?"); string num = Console.ReadLine(); Console.WriteLine("In what base is this number?"); int mathBase = int.Parse(Console.ReadLine()); double output = 0; int j = 0; char[] nums = num.ToCharArray(); for(int i=num.Length - 1; i>=0; i--) { output = output + Math.Pow(mathBase,i) * Int32.Parse(nums[j].ToString()) * 1; Console.WriteLine("i: " + i +", j:" + j + ", nums[j]: " + nums[j] + ", output: " + output + ", mathBase: " + mathBase + ", " + Math.Pow(mathBase,i) + "."); j++; } Console.WriteLine("The number " + num + " in base 10 (Decimal) is " + output + "."); Console.ReadLine(); } } }
21st Mar 2017, 7:35 AM
Eldar Bakerman
+ 1
how do you convert integer to string in c++
21st Mar 2017, 12:34 PM
Thabiso Mcd Mohlomi
Thabiso Mcd Mohlomi - avatar
+ 1
every integer can be string: int num = 3; string nums = num;
21st Mar 2017, 2:12 PM
Eldar Bakerman
+ 1
BTW it's C#
21st Mar 2017, 2:12 PM
Eldar Bakerman