Uploaded by Yash Patel

coa aarav

advertisement
AARAV LAD
12102040701002
Practical -1
Q1. Write a C program to perform the following conversions:
a) Decimal to Binary
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num[10], n, i;
printf("Enter the decimal number to convert: ");
scanf("%d", &n);
for (i = 0; n > 0; i++)
{
num[i] = n % 2;
n = n / 2;
}
printf("\nBinary of Given Number is=");
for (i = i - 1; i >= 0; i--)
{
printf("%d", num[i]);
}
return 0;
}
Output:
AARAV LAD
12102040701002
b) Decimal to Hexadecimal
#include <stdio.h>
int main()
{
long decimal, quotient, rem;
int i, j = 0;
char hexadecimal[100];
printf("Enter decimal number: ");
scanf("%ld", &decimal);
quotient = decimal;
while (quotient != 0)
{
rem = quotient % 16;
if (rem < 10)
hexadecimal[j++] = 48 + rem;
else
hexadecimal[j++] = 55 + rem;
quotient = quotient / 16;
}
printf("the hexadecimal is : ");
for (i = j; i >= 0; i--)
printf("%c", hexadecimal[i]);
return 0;
}
Output:
AARAV LAD
12102040701002
c) Binary to Decimal
#include <stdio.h>
void main()
{
int num, binary, decimal = 0, base = 1, rem;
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num);
binary = num;
while (num > 0)
{
rem = num % 10;
decimal = decimal + rem * base;
num = num / 10;
base = base * 2;
}
printf("The Binary number is = %d \n",binary);
printf("Its decimal equivalent is = %d \n", decimal);
return 0;
}
Output:
Download