EBK DATA STRUCTURES AND ALGORITHMS IN C
EBK DATA STRUCTURES AND ALGORITHMS IN C
4th Edition
ISBN: 9781285415017
Author: DROZDEK
Publisher: YUZU
bartleby

Concept explainers

Question
Book Icon
Chapter 4, Problem 2PA
Program Plan Intro

Program to convert a decimal number into any base between “2” and “9”:

Program Plan:

  • In “main()” function
    • Declare variables “stackk1” for stack, “base_to” for base to which convert and “numm” for number in decimal to be convert.
    • Read values for “base_to” and “numm” from user.
    • If base is between “2” and “9” and “numm” is greater than “0”,
      • Push the value of “numm” mod “base_to” into stack “stackk1”.
      • Then Divide “numm” by “base_to” and take this value as next “numm”.
      • Continue above two steps till “numm” becomes “0”.
    • Pop all numbers from “stackk1” and print it till “stackk1” becomes empty.
    • If base is not between “2” and “9”, print error message.

Program Plan Intro

Modified program to convert a decimal number into any base between “11” and “27”:

Program Plan:

  • In “main()” function
    • Declare variables “stackk1” for stack, “base_to” for base to which convert and “numm” for number in decimal to be convert.
    • Read values for “base_to” and “numm” from user.
    • If base is between “11” and “27” and “numm” is greater than “0”,
      • Push the value of “numm” mod “base_to” into stack “stackk1”.
      • Then Divide “numm” by “base_to” and take this value as next “numm”.
      • Continue above two steps till “numm” becomes “0”.
    • Pop each number “ele” from “stackk1”
      • If “ele” is less than or equal to “9”, print itself.
      • If “ele” is greater than “9” and less than “27”, print character corresponding to ASCII value of “ele+55”
    • If base is not between “11” and “27”, print error message.

Blurred answer
Students have asked these similar questions
A number in base 2 (binary) is a number such that each of its digits is 0 or 1. To convert from binary to decimal (base 10), the digits starting from the right are multiplied by powers of 2 (starting at 0) and added. For example, the value in decimal of 10011 is calculated as follows: 1 ×2º+ 1 ×21+ 0 ×2²+ 0 ×2³+ 1 ×24=19 Write a program that reads from the user a 5-digit integer representing a value in binary and displays its equivalent value in decimal. Note that if the user enters an integer which digits are other than 0 or 1, the program displays a message stating that the number is not valid. Sample Run 1: Enter a 5-digit integer made of Os and ls: 10011 10011 in decimal is 19 Sample Run 2: Enter a 5-digit integer made of Os and 1s: 13001 13001 is not valid
A number in base 2 (binary) is a number such that each of its digits is 0 or 1. To convert frombinary to decimal (base 10), the digits starting from the right are multiplied by powers of 2(starting at 0) and added. For example, the value in decimal of 10011 is calculated as follows: 1 ×20+ 1 ×21+ 0 ×22+ 0 ×23+ 1 ×24=19 Write a program that reads from the user a 5-digit integer representing a value in binary anddisplays its equivalent value in decimal. Note that if the user enters an integer which digits areother than 0 or 1, the program displays a message stating that the number is not valid.
A number in base 2 (binary) is a number such that each of its digits is 0 or 1. To convert frombinary to decimal (base 10), the digits starting from the right are multiplied by powers of 2(starting at 0) and added. For example, the value in decimal of 10011 is calculated as follows:
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education