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

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; }
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 *...
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...
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...
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...
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 complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if statements. . Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. • for input of "xxcaazz", "xxbaaz" → 3 • for input of "abc", "abc" → 2 • for input...
PLEASE WRITE IN C++ only . The objective of this assignment is to gain an understanding...
PLEASE WRITE IN C++ only . The objective of this assignment is to gain an understanding of the lexical analysis phase of a compiler and the process of constructing a symbol table. Problem: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a program (in C, C++, C#, Java, or Python) that implements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT