Question

Write a C++ program that asks the user to input a decimal integer (i) and an...

Write a C++ program that asks the user to input a decimal integer (i) and an integer radix from 2 to 16 (r) and outputs the value of iin radix r(base r). (HINT: use repeated division by base r)

NOTE: show output screenshot

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

CODE

#include <iostream>

#include <cstring>

using namespace std;

// To return char for a value. For example '2'

// is returned for 2. 'A' is returned for 10. 'B'

// for 11

char reVal(int num)

{

  if (num >= 0 && num <= 9)

    return (char)(num + '0');

  else

    return (char)(num - 10 + 'A');

}

// Utility function to reverse a string

void strev(char *str)

{

  int len = strlen(str);

  int i;

  for (i = 0; i < len/2; i++)

  {

    char temp = str[i];

    str[i] = str[len-i-1];

    str[len-i-1] = temp;

  }

}

// Function to convert a given decimal number

// to a base 'base' and

char* fromDeci(char res[], int base, int inputNum)

{

  int index = 0; // Initialize index of result

  // Convert input number is given base by repeatedly

  // dividing it by base and taking remainder

  while (inputNum > 0)

  {

    res[index++] = reVal(inputNum % base);

    inputNum /= base;

  }

  res[index] = '\0';

  // Reverse the result

  strev(res);

  return res;

}

// Driver program

int main()

{

  int inputNum, base;

cout << "Enter a decimal number: ";

cin >> inputNum;

cout << "Enter a base between 2 and 16: ";

cin >> base;

  char res[100];

  printf("Equivalent of %d in base %d is "

    " %s\n", inputNum, base, fromDeci(res, base, inputNum));

  return 0;

}

​​​​​​​

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that asks the user to input a decimal integer (i) and an...
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
  • Using java Sample Outputs Write a program which asks the user for an integer and then...

    Using java Sample Outputs Write a program which asks the user for an integer and then prints the following patterns based on that integer. Input Validation For Pattern 1, the input must be a value between 1 and 999 For Pattern 2, the input must be a value between 1 and 26. Requirements: For Pattern 1: must be able to work with up to three digit numbers. You will want to use printf to get the spacing correct. For Pattern...

  • Exercise 1: Write a C++ program that asks the user to input an integer n followed...

    Exercise 1: Write a C++ program that asks the user to input an integer n followed by n other characters that will be stored in an array. We suppose here that the user will input only lowercase characters between ‘a’ and ‘z’. We also suppose that the user will input different characters. The program will then sort these characters according to an increasing order of their alphabetical order. In this exercise, feel free to use selection sort, insertion sort, or...

  • Write a C Program that asks the user to input a string and then the user...

    Write a C Program that asks the user to input a string and then the user is prompted back whether the input string is a “Palindrome Word” or not. (A Palindrome Word reads the same backward or forward, eg. madam, racecar, etc.) Your program should contain a function that accepts the input string from the main function and returns a value indicating whether the input string is a Palindrome or not. Use Pointers to traverse the string.

  • Java Question, I need a program that asks a user to input a string && then...

    Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...

  • C++ Write a program that asks user to input three positive integers one at a time,...

    C++ Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation. Example output: Enter an integer: -7 Invalid! Number must be positive. Enter an integer: -2 Invalid! Number must be positive. Enter an integer: 5 Enter an integer: 7 Enter an integer: 1 Largest number: 7

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • Write a C++ program that asks the user for 50 integer numbers and an integer n....

    Write a C++ program that asks the user for 50 integer numbers and an integer n. The program will show then the pairs of numbers (a,b) where a=nb.

  • Write a program that asks the user to input a 4-digit integer and then prints the...

    Write a program that asks the user to input a 4-digit integer and then prints the integer in reverse. Your program needs to implement a function reverse that will take in as input the 4-digit number and print it out in reverse. Your code will consist of two files: main.s and reverse.s. Main.s will ask for the user input number and call the function reverse. For ARM/Data Structure needs to be able to run on Pi Reverse.s will take in...

  • Write a program which asks the user to enter an integer. Use switch statement to write...

    Write a program which asks the user to enter an integer. Use switch statement to write out the numeric word (such as ONE) if the user's input is 1; (see the sample run output for the usr input 2 or 3); otherwise, write OUT OF RANGE. Below are few sample runs: If the user enters a 1, the program will print: ONE TWO THREE Or, if the user enters a 2, the program will print: TWO THREE Or, if the...

  • use C++ please 1. Vowels and Consonants Write a program that asks the user to input...

    use C++ please 1. Vowels and Consonants Write a program that asks the user to input three different integers. Write a function called numberStyle for this program that will accept each integer (one at a time) and return the following . If the integer is even, return 1 . If the integer is odd, return -1 . If the integer is zero, return O In main, after calling the function output the appropriate message describing the integers ing the function...

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
ADVERTISEMENT