Previous Page

Base Conversion - C++ console

Copy and Paste the following code into your favorite C++ Integrated Development Environment (IDE) - compile and run.

/*
  Name: Brian Butler
  Class: 12:30am - 1:45 
  Professor: Gary Hartell 
  Date Finished: September 16, 2011

                            Description
        This program takes a number as input and then its base and
        converts it to another base.

                              Algorithm
        1. Prompt user to input an integer
        2. Prompt user to input that number's base
        3. Prompt user to input a number for the base they want to
           convert to.
        4. Do appropriate calculations to first convert their number to base 10
        5. Do appropriate calculations to then convert that base 10 number to
           the base they chose
        6. Output the new number
*/

#include <iostream>
#include <cmath>
#include <conio.h>

using namespace std;

int main()
{
     int integerNum;   /* users input number                                        */
     int x;            /* is set to integerNum and its value will change            */
     int n;            /* this will be set to the users input number and will not
                          change. It will only be used in the final output          */
     int remainder;
     int numOfDigitsEntered = 0;/*this will be the number of digits input by user   */
     int base;         /* this is the base (input by the user)
                          of the number entered by the user                         */
     int baseConvert;  /* this is the base (input by the user) that they want
                          their number converted to                                 */
     int baseTen = 0;  /* used to find the baseTen of the users input               */
     int convertedNumberIs;
     int y = 0;        /* will be equal to the base 10 number of the users input    */
     int newNumDigits = 0;/* will be the number of digits in the new base number    */
     int r;            /* will equal y which will be the base 10 number of the users
                          input number                                              */


      cout << "Enter an integer: " ;
      cin >> integerNum; /*sets integerNum to the users input                       */
      endl (cout);

      cout << "Now enter its base: ";
      cin >>  base;
      endl (cout);

      cout << "Enter the base you would like to convert to: ";
      cin >>baseConvert;
      endl (cout);

      x = integerNum;/* sets x = to the users number entered. this number will change
                        the value of it so this is why we use another variable      */
      n = integerNum;/*only for output purposes since variable n will not change    */

/************* Find the number of digits of the number entered **********************/

          while (x > 0)/* while the users integer number is greater than 0 perform
                      the loop                                                      */
          {
              x  = x / 10;/* each time the number is divided by 10, we know that it is
                        going to have another digit....x must be an integer. Once x
                        can no longer be an integer value it kicks out of this loop
                        body                                                        */

              numOfDigitsEntered = numOfDigitsEntered + 1;
          }//end while loop

/************** Convert the user's number into a base 10 number first ***************/

          while (numOfDigitsEntered >= 0)
          {

               remainder = integerNum % (int) pow(10, numOfDigitsEntered);
               /* if the numOfDigitsEntered is 4 then this takes the users input
                  MOD 10^4 which leaves the remainder                               */

               integerNum = integerNum /(int) pow(10,numOfDigitsEntered);
               /* if the numOfDigitsEntered is 4 then this takes the users input and
                  divides it by 10 ^ 4 (which is 10000)                             */

               baseTen = integerNum * (int) pow(base,numOfDigitsEntered);
			   /* this finds the products to be added together to find the base 10 of
			      the inputted int                                                  */

			   y = y + baseTen; /* this adds the products found while going through
			                    the loop                                            */

               integerNum = remainder; /* starts the loop over again with the
                                          remainder                                 */

               numOfDigitsEntered = numOfDigitsEntered - 1;
          }//end while loop

		   int simpleArray[100];/* this array will store the digits of our answer.  */



           r = y;  /* r is now set to equal the base 10 of the original number
                     entered                                                        */
/********** Find the number of digits of the new base 10 number *********************/
		  while (r>0) /* this while loop will find the number of digits in of the
		               new base                                                     */
		  {
			   r = r / baseConvert;
		       newNumDigits = newNumDigits + 1;/* the newNumDigits are the number of
			                                   digits in the new base which is
			                                   needed in the FOR LOOP in the next
			                                   while statement                      */
		  }//end while loop

/************* Convert the base 10 number into the base chosen by the user **********/

          while (y > 0) /* y is the base 10 number of the number and base entered
		                 by user                                                    */
		  {
               for(int i=0; i < newNumDigits; i++)
               {
                    convertedNumberIs = y % baseConvert;/* the remainder of this MOD
                                                           will be one of the digits
                                                           in the answer. Gets them in
                                                           reverse order though     */

                    simpleArray[i] = convertedNumberIs;/*adds the digit to the Array*/

		          y = y / baseConvert; /* this finds the quotient of the first
		                                  calculation which is used to get the next
		                                  digit                                     */
               }//end for loop
          }//end while loop

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

          cout << "The number " << n << " with a base of " << base
               << " converted to a base of " << baseConvert << " is ";

          for (int i = newNumDigits -1; i >= 0; i--)/* this for loop is needed to
                                                       print the simpleArray backwards
                                                       since our calculation of
                                                       (convertedNumberIs)gives us the
                                                       last number first            */
          {
               cout << simpleArray[i]; /* this outputs each digit from the array one
                                          at a time until no more digits are left   */
          }

    endl (cout);
    endl (cout);
    cout << "Please press any key to EXIT!" << endl;
    getch();

    return 0;
}

//END OF CODE

Below is a sample input/output of this program.

sample base conversion output