C PROGRAM TO CONVERT BINARY TO OCTAL,HEXADECIMAL AND VICE VERSA

A)     Binary to Decimal:

#include <stdio.h> #include<conio.h>

 

int main()

{

int num, binary_val, decimal_val = 0, base = 1, rem;

 

printf("\nEnter a binary number(1s and 0s) \n"); scanf("%d", &num);

binary_val = num; while (num > 0)

{

rem = num % 10;

decimal_val = decimal_val + rem * base; num = num / 10 ;

base = base * 2;

}

printf("\nThe Binary number is = %d \n", binary_val); printf("\nIts decimal equivalent is = %d \n", decimal_val);


 

getch();

return 0;

 

}

B)     Binary to Octal: #include <stdio.h> #include <conio.h>

 

int main()

{

long int binarynum, octalnum = 0, j = 1, remainder; 

clrscr();

 

printf("Enter the value for binary number: "); scanf("%ld", &binarynum);

while (binarynum != 0)

{

remainder = binarynum % 10; octalnum = octalnum + remainder * j;

j = j * 2;

binarynum = binarynum / 10;

}

printf("Equivalent octal value: %lo", octalnum); getch();

return 0;

}

 

C)   Binary to Hexadecimal:

#include <stdio.h> #include <conio.h> int main()

{

long int binaryval, hexadecimalval = 0, i = 1, remainder; clrscr();

printf("\nEnter the binary number: "); scanf("%ld", &binaryval);

while (binaryval != 0)

{


 

remainder = binaryval % 10;

hexadecimalval = hexadecimalval + remainder * i; i = i * 2;

binaryval = binaryval / 10;

}

printf("\nEquivalent hexadecimal value: %lX", hexadecimalval); getch();

return 0;

}

 

 

 D) Number to Binary:

#include<stdio.h> #include<conio.h>

int main()

{

int number, n, remainder, binary = 0, place = 1;

clrscr();

printf("\nEnter a number :"); scanf("%d", &number);

n = number; while (n >0)

{

remainder = n % 2;

binary = binary + remainder * place; place = place * 10;

n = n/2;

}

printf("\nBinary equivalent of %d is %d", number, binary);

getch();

return 0;

}

 

 

 

Comments

Popular posts from this blog