  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Number System in Computer

Last Updated on April 11, 2023 by Prepbytes We communicate using words and characters. We are familiar with numbers, characters, and words. However, this type of data is incompatible with computers. Computers can only comprehend numbers. As a result, when we enter information, it is converted into an electronic pulse. ASCII converts the code from each pulse into a numeric format. It assigns a numerical value (number) to each number, character, and symbol. So, to understand computer language, one must be familiar with number systems. Here we explore more about the number system in computers, types of the number system in computers, and conversion of the number system in computers.

## What is a Number System?

A number system is a method of writing numbers that is a mathematical way of representing the numbers of a given set by mathematically using numbers or symbols. A number system is defined as a writing system for denoting numbers logically using digits or symbols. The numeral system represents a useful set of numbers, reflects a number’s arithmetic and algebraic structure, and offers standard representation. All numbers can be formed using the digits 0 to 9. Anyone can generate an infinite number using these digits. For instance, 156,3907, 3456, 1298, 784859, and so on.

## Number System in Computer

Number system in computer The most commonly used number system is the binary system, also known as the base-2 number system. The binary system uses only two digits, 0 and 1, to represent all numbers and data.

Binary numbers are used extensively in digital electronics and computing because it is easy to represent them using switches, where 0 represents "off" and 1 represents "on." This makes it possible to store and manipulate data electronically using digital circuits.

These systems are often used for representing and manipulating large binary numbers or for expressing memory addresses in a compact and easy-to-read format. Computer programs and programming languages use number system concepts to perform arithmetic operations, store data, and communicate with hardware devices.

## Types of the Number System in Computer

There are mainly four types of the number system in computer • Binary Number System: The binary number system is the most fundamental number system used in computer science. It uses only two digits, 0 and 1, to represent all numbers and data.
• Decimal Number System: The decimal number system is also used in computer science, but it is not as fundamental as the binary system. It uses ten digits, 0 through 9, to represent numbers.
• Octal Number System: The octal number system uses eight digits, 0 through 7, to represent numbers. It is commonly used in computer programming and digital electronics.
• Hexadecimal Number System: The hexadecimal number system uses 16 digits, including 0 through 9 and A through F, to represent numbers. It is often used in computer programming and digital electronics. Each of these number systems has its advantages and disadvantages, and each is used in different applications. For example, the binary number system is used to represent and manipulate data in computer hardware, while the decimal number system is used for everyday calculations. The octal and hexadecimal systems are commonly used in computer programming and digital electronics because they are more compact and easier to read than binary.

## Conversion of the Number System in the Computer

Converting between number system in computer science is an important skill, especially when working with binary, octal, and hexadecimal numbers. Here are the basic steps to convert a number from one number system to another:

Understand the values of each digit in the current number system: Before you can convert a number, you need to understand how the current number system works. For example, in the binary system, each digit represents a power of two, starting with 20 on the right side.

Convert the number to decimal: To convert a number from any base to decimal, you need to multiply each digit by its corresponding power of the base and add up the results. For example, to convert the binary number 1011 to decimal, you would calculate (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20) = 8 + 0 + 2 + 1 = 11.

Check your answer: To verify your answer, you can convert the number back to the original base and make sure it matches the original number.

Example: Convert the decimal number 158 to binary, octal, and hexadecimal.
Below is the conversion one by one:

• Binary Conversion:
To convert a decimal number to binary, we divide the decimal number by 2 repeatedly and write the remainder in reverse order.

158 / 2 = 79 remainder 0
79 / 2 = 39 remainder 1
39 / 2 = 19 remainder 1
19 / 2 = 9 remainder 1
9 / 2 = 4 remainder 1
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1

Therefore, the binary equivalent of 158 is 10011110.

• Octal Conversion:
To convert a decimal number to an octal, we divide the decimal number by 8 repeatedly and write the remainder in reverse order.

158 / 8 = 19 remainder 6
19 / 8 = 2 remainder 3
2 / 8 = 0 remainder 2

Therefore, the octal equivalent of 158 is 236.

To convert a decimal number to hexadecimal, we divide the decimal number by 16 repeatedly and write the remainder in reverse order. For remainders greater than 9, we use letters A-F.

158 / 16 = 9 remainder 14 (E)
9 / 16 = 0 remainder 9

Therefore, the hexadecimal equivalent of 158 is 9E.

Output

``````Decimal: 158
Binary: 10011110
Octal: 236

Code Implementation

```#include <iostream>
#include <stack>
using namespace std;

void decimalToBinary(int decimalNum) {
stack<int> binaryNum;
while(decimalNum > 0) {
binaryNum.push(decimalNum % 2);
decimalNum /= 2;
}
cout << "Binary equivalent: ";
while(!binaryNum.empty()) {
cout << binaryNum.top();
binaryNum.pop();
}
cout << endl;
}

void decimalToOctal(int decimalNum) {
stack<int> octalNum;
while(decimalNum > 0) {
octalNum.push(decimalNum % 8);
decimalNum /= 8;
}
cout << "Octal equivalent: ";
while(!octalNum.empty()) {
cout << octalNum.top();
octalNum.pop();
}
cout << endl;
}

stack<char> hexNum;
while(decimalNum > 0) {
int remainder = decimalNum % 16;
if(remainder < 10) {
hexNum.push(remainder + '0');
}
else {
hexNum.push(remainder - 10 + 'A');
}
decimalNum /= 16;
}
while(!hexNum.empty()) {
cout << hexNum.top();
hexNum.pop();
}
cout << endl;
}

int main() {
int decimalNum;
cout << "Enter a decimal number: ";
cin >> decimalNum;
decimalToBinary(decimalNum);
decimalToOctal(decimalNum);
return 0;
}
```

Input

``158``

Output

``````Enter a decimal number: Binary equivalent: 10011110
Octal equivalent: 236

Explanation:
The conversion of a number system in computer involves dividing the given number by the base of the target number system and writing the remainder in reverse order. For binary, the base is 2, for octal, it is 8, and for hexadecimal, it is 16. In each step, we divide the quotient obtained in the previous step by the base of the target number system and take the remainder. We continue this process until the quotient becomes zero. Finally, we write the remainder obtained in reverse order to get the equivalent number in the target number system. The program first prompts the user to enter a decimal number. It then calls three different functions for converting the decimal number to binary, octal, and hexadecimal. The functions use a stack data structure to store the remainder obtained during the division method of conversion. Finally, the program outputs the equivalent values in binary, octal, and hexadecimal.

Conclusion
Number system theory studies mathematical systems that describe how numbers are represented and manipulated. There are several types of number systems, including the decimal system, binary system, octal system, and hexadecimal system, among others. The theory of number systems includes concepts such as place value, conversion between different number systems, addition, subtraction, multiplication, and division. It also includes advanced topics such as modular arithmetic, number theory, and cryptography. In computer science, number system theory is crucial in digital electronics, programming languages, and computer networking. Understanding the theory of number systems is essential for anyone who wants to work in these fields. It also has applications in various other fields, including finance, physics, and engineering.

Here are the FAQs on the number system in computer

Q1: Why is the binary number system used in computers?
Ans: Computers use binary number system because it is the simplest number system to implement using electronic components. Binary system can represent all data and instructions in a computer as a combination of 0 and 1, which can be easily represented using electronic switches.

Q2: How are numbers converted between binary, decimal and hexadecimal systems?
Ans: To convert a binary number to a decimal number, you can use the method of multiplying each digit by the power of 2, starting from the rightmost digit, and adding up the results. To convert a decimal number to a binary number, you can use the method of dividing the decimal number by 2 and noting the remainder at each step. To convert between hexadecimal and binary, you can first convert the hexadecimal number to its equivalent binary number, and vice versa.

Q3: What is the significance of different number systems in computer programming?
Ans: Different number systems are used in computer programming to represent and manipulate data in different ways. For example, the hexadecimal number system is often used in computer programming because it represents a more compact way of representing binary data. The choice of number system depends on the specific application and requirements of the computer system.

Q4: What is the purpose of using different number systems in computers?
Ans: Different number systems are used in computers to represent and manipulate data in different ways. For example, the binary number system is used to represent data in a computer’s memory and in communication between computer devices, while the hexadecimal number system is often used in computer programming. The choice of number system depends on the specific application and requirements of the computer system.