Question

In: Computer Science

In C programming Generalize the to binary() function of Listing 9.8 (from your textbook) to a...

In C programming

Generalize the to binary() function of Listing 9.8 (from your textbook) to a to base n(number, base) function that takes a second argument in the range 2–10. It then should convert (and print) the number that is its first argument to the number base given by the second argument. For example, to base n(129,8) would display 201, the base-8 equivalent of 129. Test the function in a complete program.

Solutions

Expert Solution

#include<stdio.h>
#include<math.h>  //FOR POW FUNCTION

int toBaseN(int number, int base)
{
        int j, convertedNumber = 0, tmp, digit;

        j = 0;
        tmp = number;
        while(tmp != 0)
        {
                digit = tmp % base;
                tmp = tmp / base;
                convertedNumber = convertedNumber + (digit * pow(10, j));
                j++;
        }

        return convertedNumber;
}

int main()
{
        int number, base, convertedNumber;

        //TAKING INPUT FROM USER
        printf("Enter the number: ");
        scanf("%d", &number);
        printf("\nEnter the base in which you want to convert(2 - 10): ");
        scanf("%d", &base);

        //CONVERTING THE NUMBER TO BASE N
        convertedNumber = toBaseN(number, base);

        //PRINTING THE RESULT
        printf("\nThe number %d in base %d is: %d\n", number, base, convertedNumber);
}

NOTE: This is the required C program, and if you are having problem in running the program, please comment.

PLEASE UPVOTE IF YOU LIKED THE ANSWER.


Related Solutions

C# Programming create a Hash Function
C# Programming create a Hash Function
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences of t in s to u, result stored in v * * Example: * * char v[100]; * replace("hello", "el", "abc", v) => v becomes "habclo" * replace("", "el", "abc", v) => v becomes "" (no change) * replace("hello", "abc", "def", v) => v becomes "hello" (no change) * replace("utilities", "ti", "def", v) => v becomes "udeflidefes" * */ void replace(char *s, char *t,...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
Assembly Language Programming Exercise 5. Listing File for AddTwoSum ( 5 pts ) Generate a listing...
Assembly Language Programming Exercise 5. Listing File for AddTwoSum ( 5 pts ) Generate a listing file for the AddTwoSum program and write a description of the machine code bytes generated for each instruction. You might have to guess at some of the meanings of the byte values. Hint: Watch my tutorial and read a little bit of Ch 4.
Listing the muscles of the shoulder, their origin, insertion and function Identifying and listing the boundaries...
Listing the muscles of the shoulder, their origin, insertion and function Identifying and listing the boundaries of the axillary fossa Contrasting the components of the quadrangular space, triangular space and triangular interval Listing the muscles of the arm, their origin, insertion and function Listing the muscles of the forearm and their function Organizing the muscles of the forearm based on their respective compartments Organizing the muscles of the forearm based on their respective layers Listing the boundaries of the cubital...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT