0% found this document useful (0 votes)
14 views11 pages

C Program for Number System Conversions

This C program allows users to convert numbers between binary, decimal, octal, and hexadecimal systems, as well as perform inverse conversions. It includes functions for each conversion type and a main function that prompts the user to select an input number system and enter a number. The program outputs the converted values based on the user's choice.

Uploaded by

ishantiwari880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

C Program for Number System Conversions

This C program allows users to convert numbers between binary, decimal, octal, and hexadecimal systems, as well as perform inverse conversions. It includes functions for each conversion type and a main function that prompts the user to select an input number system and enter a number. The program outputs the converted values based on the user's choice.

Uploaded by

ishantiwari880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C PROGRAMMING PROJECT

ISHAN TIWARI
E22BCAU0007

CODE :- For coverting binary, decimal,


octal, and hexadecimal, as well as
their respective inverse conversions:

#include <stdio.h>
#include <string.h>

// Function to convert binary to decimal


int binaryToDecimal(char *binary) {
int decimal = 0;
int base = 1;
int i = 0;
int size = strlen(binary);
for (i = size - 1; i >= 0; i--) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}

return decimal;
}

// Function to convert decimal to binary


void decimalToBinary(int decimal) {
int binary[32];
int i = 0;

while (decimal > 0) {


binary[i] = decimal % 2;
decimal /= 2;
i++;
}

printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

// Function to convert decimal to octal


void decimalToOctal(int decimal) {
int octal[100];
int i = 0;

while (decimal > 0) {


octal[i] = decimal % 8;
decimal /= 8;
i++;
}

printf("Octal: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", octal[j]);
}
printf("\n");
}

// Function to convert decimal to hexadecimal


void decimalToHexadecimal(int decimal) {
char hexadecimal[100];
int i = 0;

while (decimal > 0) {


int remainder = decimal % 16;
if (remainder < 10) {
hexadecimal[i] = remainder + '0';
} else {
hexadecimal[i] = remainder + 55; // 'A' - 10
}
decimal /= 16;
i++;
}

printf("Hexadecimal: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexadecimal[j]);
}
printf("\n");
}

// Function to convert octal to decimal


int octalToDecimal(char *octal) {
int decimal = 0;
int base = 1;
int i = 0;
int size = strlen(octal);

for (i = size - 1; i >= 0; i--) {


decimal += (octal[i] - '0') * base;
base *= 8;
}

return decimal;
}

// Function to convert hexadecimal to decimal


int hexadecimalToDecimal(char *hexadecimal) {
int decimal = 0;
int base = 1;
int i = 0;
int size = strlen(hexadecimal);

for (i = size - 1; i >= 0; i--) {


if (hexadecimal[i] >= '0' && hexadecimal[i] <= '9') {
decimal += (hexadecimal[i] - '0') * base;
} else if (hexadecimal[i] >= 'A' && hexadecimal[i] <= 'F') {
decimal += (hexadecimal[i] - 55) * base; // 'A' - 10
} else if (hexadecimal[i] >= 'a' && hexadecimal[i] <= 'f') {
decimal += (hexadecimal[i] - 87) * base; // 'a' - 10
}
base *= 16;
}

return decimal;
}

// Function to convert decimal to octal


void decimalToOctal(int decimal) {
int octal[100];
int i = 0;

while (decimal > 0) {


octal[i] = decimal % 8;
decimal /= 8;
i++;
}

printf("Octal: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", octal[j]);
}
printf("\n");
}

// Function to convert hexadecimal to binary


void hexadecimalToBinary(char *hexadecimal) {
char binary[100];
int decimal = hexadecimalToDecimal(hexadecimal);
int i = 0;

while (decimal > 0) {


binary[i] = decimal % 2 + '0';
decimal /= 2;
i++;
}

printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", binary[j]);
}
printf("\n");
}

// Function to convert octal to binary


void octalToBinary(char *octal) {
char binary[100];
int decimal = octalToDecimal(octal);
int i = 0;

while (decimal > 0) {


binary[i] = decimal % 2 + '0';
decimal /= 2;
i++;
}

printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", binary[j]);
}
printf("\n");
}

int main() {
int choice;
char number[100];

printf("Select the input number system:\n");


printf("1. Binary\n");
printf("2. Decimal\n");
printf("3. Octal\n");
printf("4. Hexadecimal\n");
printf("Enter your choice: ");
scanf("%d", &choice);

printf("Enter a number: ");


scanf("%s", number);

switch (choice) {
case 1: { // Binary
int decimal = binaryToDecimal(number);
printf("Decimal: %d\n", decimal);
decimalToOctal(decimal);
decimalToHexadecimal(decimal);
break;
}
case 2: { // Decimal
int decimal = atoi(number); // Convert string to integer
decimalToBinary(decimal);
decimalToOctal(decimal);
decimalToHexadecimal(decimal);
break;
}
case 3: { // Octal
int decimal = octalToDecimal(number);
printf("Decimal: %d\n", decimal);
decimalToBinary(decimal);
decimalToHexadecimal(decimal);
break;
}
case 4: { // Hexadecimal
int decimal = hexadecimalToDecimal(number);
printf("Decimal: %d\n", decimal);
decimalToBinary(decimal);
decimalToOctal(decimal);
break;
}
default:
printf("Invalid choice!\n");
break;
}

return 0;
}
---------------------THANK YOU-------------------

You might also like