Question3: Base conversion Write a program using a loop that asks the user to en
ID: 3934839 • Letter: Q
Question
Question3: Base conversion Write a program using a loop that asks the user to enter a number base 4, converts it to the corresponding decimal number, and prints the decimal number. Note that a the digits of a number base 4 is in the range 0-3. Example: Input: 1223 Output: 107 Note: 1x43 + 2x42 + 2x4 + 3x1 = 107 Step1: 1223 % 10 = 3 1223 / 10 = 122 3*1 Step2: 122 % 10 = 2 122 / 10 = 12 2*4 Step3: 12 % 10 = 2 12 / 10 = 1 2*42 Step4: 1 % 10 = 1 1 / 10 = 0 1*43 107 Notes: Your program should work for any number, not only for the example given above. Do not display the steps. I gave you the steps to clarify the problem.
Explanation / Answer
// C++ code convert number in base 4 to base 10
#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int number;
int i = 0;
int result = 0;
cout << "Enter number in base 4: ";
cin >> number;
while(number != 0)
{
int temp = number%10;
result = result + temp*pow(4,i);
i++;
number = number/10;
cout << result << endl;
}
cout << "Number in base 10: " << result << endl;
return 0;
}
/*
output:
Enter number in base 4: 1223
Number in base 10: 107
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.