Decimal to binary bitwise
#include < stdio.h >
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d", &n);
printf("%d in binary number system is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)//k is logically ANDed with 1
printf("1");
else
printf("0");
}
return 0;
}
Decimal to Binary
#include < stdio.h>
void main()
{
long num, decimal_num, remainder, base = 1, binary = 0;
printf("Enter a decimal integer \n");
scanf("%ld", & num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Its binary equivalent is = %ld\n", binary);
}
Decimal to Octal
#include
void main()
{
long num, decimal_num, remainder, base = 1, octal = 0;
printf("Enter a decimal integer \n");
scanf("%ld", & num);
decimal_num = num;
while (num > 0)
{
remainder = num % 8;
octal = octal + remainder * base;
num = num / 8;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Its octal equivalent is = %ld\n", Octal);
}
Binary to Decimal
#include < stdio.h >
void main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
printf("Enter 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("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
}
Binary to Octal
#include < stdio.h>
int main()
{
long int binarynum, octalnum = 0, j = 1, remainder;
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);
return 0;
}
Binary to Hexadecimal
#include < stdio.h>
int main()
{
long int binaryval, hexadecimalval = 0, i = 1, remainder;
printf("Enter the binary number: ");
scanf("%ld", &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("Equivalent hexadecimal value: %lX", hexadecimalval);
return 0;
}
These are the Concept of C programming codes... We'll be glad if you share your valuable information with us regarding this topic, then it will be a golden opportunity for all of us to improve ourselves and know more. Comment below!!
Comments
Post a Comment