Professional Documents
Culture Documents
com/c-programs/)
Table of Content
C Program to Convert Decimal number into Binary Number
(http://www.c4learn.com/c-programs/program-to-convert-decimal-number-into.html)
C Program to Convert Decimal number to Octal Number
(http://www.c4learn.com/c-programs/program-for-decimal-number-to-octal.html)
C Program to Convert Decimal Number to Hexadecimal Number
C Program to Convert Binary to Decimal number
(http://www.c4learn.com/c-programs/program-to-convert-binary-to-decimal.html)
C Program to Convert Decimal to Binary using Bitwise AND operator
(http://www.c4learn.com/c-programs/decimal-to-binary-using-bitwise-and.html)
COMPOSE
#include<stdio.h>
#include<conio.h>
#include<math.h>
void dec_hex(long int num)
// Function Definition
{
long int rem[50],i=0,length=0;
while(num>0)
{
rem[i]=num%16;
num=num/16;
i++;
length++;
}
printf("Hexadecimal number : ");
for(i=length-1;i>=0;i--)
{
switch(rem[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default :
printf("%ld",rem[i]);
}
}
}
//================================================
void main()
{
long int num;
clrscr();
printf("Enter the decimal number : ");
scanf("%ld",&num);
dec_hex(num);
// Calling function
getch();
}
Output :
Now after accepting the number we are calling the function which can evaluate
equivalent hexadecimal number.
dec_hex(num);
// Calling function
Inside the function we have declared 3 variables. Below table will explain the significance
of each and every variable used inside the function.
Variable
num
This variable is used to store the actual decimal number. We are dividing
this variable for finding the remainder and quotient.
rem[50]
length
Now perform below steps until the number becomes less than 0
1. Divide the number with 16 and store reminder in an array
while(num > 0)
{
rem[i] = num % 16;
num = num / 16;
i++;
length++;
}
After coming out of the loop. We need to print reminder in reverse fashion. Now consider
the following table in order to print array
Remainder
Display this
10
11
12
13
14
15
In order to print reminders greater than 10 , refer above table. This can be done with
switch case.
case 10:
printf("A");
break;
case 11:
printf("B");
break;
Download Program :
[box]Download Code (http://www.box.net/shared/nlrl40u75k)[/box]
Official HP Online
Store
Buy HP Original Toner
Cartridges. Free Same Day
Delivery. Pay COD.
Get in Touch!
Recent Programs
C Program to read the content of file using fgets (http://www.c4learn.com/c-programs/readcontent-file-using-fgets.html)
C Program to perform arithmetic operations on float
(http://www.c4learn.com/c-programs/perform-arithmetic-operations-on-float.html)