Question

In: Computer Science

Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program...

Please complete in only C++, using loops

Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary. Another example, 8,943 in base 10, is 13,236 in base 9.

You’ll need to perform these operations on the following combinations:

A: 15, base 2.

B: 38, base 16.

C: 54, base 6.

D: 19, base 8.

E: 27, base 3.

Solutions

Expert Solution

Hi,

The standard number system range starts from 2 to 16 base and 10,11,12,13,14,15 are represented as A, B, C, D, E, F.

And in the question, nothing is mentioned about standard so i have attached both standard and non-standard code and you can follow any code.

--------------------------------------------------------------------

CODE 1: For given code ,The standard valid base are between 2 to 16 including 2 and 16 and valid values of 10,11,12,13,14,15 are A, B, C, D, E, F respectively.

#include<iostream>
using namespace std;
int main()
{
        int base,n,temp,count=0,i;
        char arr[100];
        cout<<"enter the number"<<endl;
        cin>>n;
        cout<<"enter the base"<<endl;
        cin>>base;
        if(base<2 || base>16)
        {
        cout<<"please enter valid base between 2 to 16 including 2 and 16 "<<endl;
        return 0;
        }
        
        while(n!=0)
        {
                temp=n%base;
                if(temp<10)
                {
                        arr[count]=temp+48;
                }
        
           else
           {
           arr[count]=temp+55;
           }
                n=n/base;
                count++;
        }
        cout<<"converted value is"<<endl;
        for(i=count-1;i>=0;i--)
        {
                cout<<arr[i];     
        }
        return 0;
}

This is snapshot of above code

This is snapshot of output for above code

----------------------------------------------------------------------------------------------

CODE 2: For given code ,The valid base are between 2 to any base.

#include<iostream>
using namespace std;
int main()
{
        int base,n,temp,count=0,i;
        int arr[100];
        cout<<"enter the number"<<endl;
        cin>>n;
        cout<<"enter the base"<<endl;
        cin>>base;
        if(base<2)
        {
        cout<<"please enter valid base greater than 1"<<endl;
        return 0;
        }       
        while(n!=0)
        {
                temp=n%base;
                arr[count]=temp;
                n=n/base;
                count++;
        }
        cout<<"converted value is"<<endl;
        for(i=count-1;i>=0;i--)
        {
                cout<<arr[i];     
        }
        return 0;
}

This is snapshot of above code.

This is snapshot of output for above code

Hope it helps.

Still if you have any doubt or need help, please let me know in comment section and if you like, Please upvote.

Thank You


Related Solutions

Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
please complete the following program in c++: In this homework assignment you will modify the program...
please complete the following program in c++: In this homework assignment you will modify the program you have developed in Homework Assignment 1 by implementing the following: 1- Your program will ask the user to choose to either Compute and Display Surface Areas of Hemispheres and their Average (Choice 1) or to Compute and Display Volumes of Hemispheres and their Average (Choice 2). The program will only perform the chosen operation. (Hint: use a character variable to read the character...
In C++ Please, using only the libraries given in this homework prompt, Write a program that...
In C++ Please, using only the libraries given in this homework prompt, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However this time the statement is designed in the style of a Logical Circuit. You are asked to create three truth tables for the three problems. To achieve this, you should create SEVEN gate functions for your checkpoint. For each of the problem: First, convert the diagram to...
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However the statement is designed in the style of a Logical Circuit.You are asked to create three truth tables for the three problems.First, convert the diagram to boolean algebra equations.Then evaluate their values step by step based on the values of A, B and C. Finally, print the...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
C++ Please Do not use loops!! Assignment : Dice Roll Redux In "Dungeons & Dragons" and...
C++ Please Do not use loops!! Assignment : Dice Roll Redux In "Dungeons & Dragons" and similar role-playing games, dice with different numbers of sides are used to determine the outcomes of events throughout the game. There are different dice for different occasions: 4-sided, 6-, 8-, 10-, 12-, and 20-sided dice are common. A required roll is typically designated <n>d<s> where <n> is the number of dice to throw and <s> is the number of sides on those dice. For...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
Write a C# Program (using loops) to read temperature of the 7 days of the week...
Write a C# Program (using loops) to read temperature of the 7 days of the week (one day at a time) and calculate the average temperature of the week and print it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT