0% found this document useful (0 votes)
75 views7 pages

C Programs for Number Base Conversions

The document contains C programs for converting binary to decimal, decimal to binary, octal to hexadecimal, and performing binary addition. Each program includes the code, aim, and sample outputs demonstrating their functionality. The results confirm successful execution of the programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views7 pages

C Programs for Number Base Conversions

The document contains C programs for converting binary to decimal, decimal to binary, octal to hexadecimal, and performing binary addition. Each program includes the code, aim, and sample outputs demonstrating their functionality. The results confirm successful execution of the programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Implement a C program to convert a binary number to decimal number.

Aim: To write a C program to convert a binary number to decimal number

Program:

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i, binnum, decimalnum = 0, rem;
printf (" Enter the binary number with the combination of 0s and 1s \n");
scanf ("%d", &bin_num);
printf( " \n The binary number is %d", binnum);
for (i = 0; binnum != 0;i++)
{
rem = binnum % 10;
binnum = binnum / 10;
decimalnum = decimalnum + (rem) * ( pow (2, i));

}
printf ("\n Conversion from binary to decimal number is %d", decimalnum);
getch();
}

Result: Hence the program is executed.


Output:
Enter the binary number with the combination of 0s and 1s
10010
The binary number is 10010
Conversion from binary to decimal number is 18
2. Implement a C program to convert a decimal number to binary number.

Aim: To write a C program to convert a decimal number to binary number

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int decimalnum, binarynum = 0, i = 1, remainder;
printf("Enter a decimal number: ");
scanf("%d", &decimalnum);
while (decimalnum != 0)
{
remainder = decimalnum % 2;
decimalnum /= 2;
binarynum += remainder * i;
i *= 10;
}
printf("Binary number: %d\n", binarynum);
getch();
}

Result: Hence the program is executed.


Output:
Enter a decimal number:15
Binary number:1111
3. Implement a C program to convert a octal number to hexadecimal number.

Aim: To write a C program to convert a octal number to hexadecimal number.

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};

long long octal, temp_Octal, binary=0, place=1;


char hex[65] = "";
int rem;
clrscr();
printf("Please Enter any Octal Number: ");
scanf("%lld", &octal);
temp_Octal = octal;

// First Convert Octal to Binary

while(temp_Octal > 0)
{
rem = temp_Octal % 10;
binary = (OCTALVALUES[rem] * place) + binary;
temp_Octal /= 10;

place *= 1000;
}
// Convert Binary to Hexadecimal

while(binary > 0)
{
rem = binary % 10000;
switch(rem)
{
case 0:
strcat(hex, "0");
break;
case 1:
strcat(hex, "1");
break;
case 10:
strcat(hex, "2");
break;
case 11:
strcat(hex, "3");
break;
case 100:
strcat(hex, "4");
break;
case 101:
strcat(hex, "5");
break;
case 110:
strcat(hex, "6");
break;
case 111:
strcat(hex, "7");
break;
case 1000:
strcat(hex, "8");
break;
case 1001:
strcat(hex, "9");
break;
case 1010:
strcat(hex, "A");
break;
case 1011:
strcat(hex, "B");
break;
case 1100:
strcat(hex, "C");
break;
case 1101:
strcat(hex, "D");
break;
case 1110:
strcat(hex, "E");
break;
case 1111:
strcat(hex, "F");
break;
}
binary /= 10000;
}
strrev(hex);
printf("Octal number: %lld\n", octal);
printf("Equivalent Hexadecimal number: %s", hex);
getch();
}

Result: Hence the program is executed.


Output:
4. Implement a C program to perform Binary addition.

Aim: To write a C program to to perform Binary addition.


Program:
#include <stdio.h>
int main()
{

long binary1, binary2;


int i = 0, remainder = 0, sum[20];
clrscr();
printf("Enter the first binary number: ");
scanf("%ld", &binary1);
printf("Enter the second binary number: ");
scanf("%ld", &binary2);
while (binary1 != 0 || binary2 != 0)
{
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
i--;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);
return 0;
}

Common questions

Powered by AI

In C, integer arithmetic directly influences binary and decimal conversions, particularly given no native support for binary data types. Conversions rely on int types which truncate values, meaning fractional parts of operations are disregarded, potentially influencing calculations. This characteristic is leveraged for simplicity in conversions — for example, int division is used to strip processed digits, and division results in whole numbers only, simplifying iterative digit processing. However, programmers must be cautious of potential overflow issues and loss of precision due to this integer-focused arithmetic approach .

The program employs the conio.h library functions like clrscr() to clear the console screen and getch() to pause the program, waiting for a user keystroke. These functions provide basic control over the user interface of console applications, enhancing user experience with a clean screen and controlled execution flow. However, they present limitations, mainly in terms of portability because conio.h is not part of the ANSI C standard, which can lead to compatibility issues on non-DOS operating systems where conio.h may be absent. Modern C development often excludes this library in favor of more portable and standard-compliant libraries .

The pow function is used in the binary to decimal conversion program to compute powers of 2, which are necessary in determining the value of each binary digit's position. By raising 2 to the power of the digit's index, the program calculates its positional value in the binary system, which is then added to the accumulated total for the decimal number. This allows the correct conversion of binary to its equivalent decimal value by taking into account the base-two weighting of binary digits .

The C program converts a binary number to a decimal number by iterating through each digit of the binary number, starting from the least significant digit. For each iteration, it calculates the remainder of the binary number when divided by 10 to isolate the current binary digit. This digit is then multiplied by 2 raised to the power of the current position index (i), and the result is added to a cumulative decimal number. The loop continues until all digits have been processed, at which point the cumulative decimal number is displayed .

Initializing variables such as decimalnum and binarynum to zero is crucial in conversion programs to ensure a clean starting state for accumulated results. Since these variables are used to store calculated totals through iterative processes, any residual data from previous operations or random memory content could lead to incorrect outputs. Proper initialization guarantees that additions and multiplications for new operations start from a known base, thus maintaining expected behavior and reliability of the program outcomes .

The program performs binary addition by processing each corresponding pair of digits from two binary numbers starting from the least significant digits. For each pair, it calculates the sum and determines the digit in the result as well as a remainder for carry-over to the next significant digit. The remainder plays a crucial role in handling the carry, whereby if the sum exceeds 1, the remainder ensures the carry is added into the next column of digits. This is achieved by taking the sum modulo 2 for the current digit and dividing the sum by 2 to determine the carry. The process continues until all digits are processed, and any remaining carry is included in the result .

The main challenge using a switch statement to convert binary to hexadecimal is ensuring that each binary segment maps correctly to its hexadecimal equivalent, especially for maintaning readability and avoiding logical errors. An error in mapping can lead to incorrect conversions. The program addresses this by explicitly defining cases for each possible four-digit binary input (from binary '0000' to '1111'), ensuring all possible binary values are covered. The choice of a fixed list of cases also minimizes potential bugs as each pathway is explicitly handled and can be thoroughly tested .

The conversion from octal to hexadecimal involves two main steps. First, the program converts the octal number to a binary number by using an array to map each octal digit to its corresponding three-digit binary equivalent. After obtaining a complete binary number, the program then converts the binary to hexadecimal. This is done by processing the binary number in groups of four digits (nibbles), each representing a single hexadecimal digit. The program uses a switch-case construct to map each four-digit binary group to a corresponding hexadecimal character, appending each character to form the final hexadecimal number .

Dividing the binary number by 10 is necessary in the conversion program to isolate each digit of the binary number from right to left, which is crucial for calculating its contribution to the decimal equivalent. This operation allows the program to process each digit individually by reducing the binary number step by step, until no digits remain to be processed .

The binary addition program uses an array to store the result of each digit-wise addition operation because binary arithmetic involves progressive carrying, where results aren't directly stored at their final positions. The array provides a flexible storage mechanism for managing intermediate results. After processing all digits from both binary numbers, the array contains the complete sum in reverse order. The final sum is obtained by iterating over the array in reverse, which aligns the calculated binary sum in its correct order for output .

You might also like