Q. Write a program to accept a decimal number and convert it to binary, octal and hexadecimal.
Write separate functions
Program
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num, choice;
printf("\nEnter Decimal Number:\t");
scanf("%d", &num);
printf("\nBinary Value :\t");
conversion(num, 2);
printf("\nOctal Value :\t");
conversion(num, 8);
printf("\nHexadecimal Value:\t");
conversion(num, 16);
return 0;
}
void conversion(int num, int base)
{
int remainder = num % base;
if(num == 0)
{
return;
}
conversion(num / base, base);
if(remainder < 10)
{
printf("%d", remainder);
}
else
{
printf("%c", remainder - 10 + 'A' );
}
}
Output:
Enter Decimal Number: 45
Binary Value : 101101
Octal Value : 55
Hexadecimal Value: 2D
Program
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num, choice;
printf("\nEnter Decimal Number:\t");
scanf("%d", &num);
printf("\nBinary Value :\t");
conversion(num, 2);
printf("\nOctal Value :\t");
conversion(num, 8);
printf("\nHexadecimal Value:\t");
conversion(num, 16);
return 0;
}
void conversion(int num, int base)
{
int remainder = num % base;
if(num == 0)
{
return;
}
conversion(num / base, base);
if(remainder < 10)
{
printf("%d", remainder);
}
else
{
printf("%c", remainder - 10 + 'A' );
}
}
Output:
Enter Decimal Number: 45
Binary Value : 101101
Octal Value : 55
Hexadecimal Value: 2D
3 Comments
how to write c program to multiple two matrix of 4*4
ReplyDeletehttps://github.com/Narendarmk/C-Programs/blob/master/matrixMultliplication
Deletethe declaration of conversion is an implicit function, causing the code to not compile correctly
ReplyDelete