0% found this document useful (0 votes)
16 views2 pages

New Text Document

The document contains code for a C program that converts a decimal number to binary and hexadecimal representations. It takes a decimal number as input, stores the binary representation in an array, and then prints the hexadecimal output by mapping binary digits above 9 to A-F characters.

Uploaded by

abhiece
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

New Text Document

The document contains code for a C program that converts a decimal number to binary and hexadecimal representations. It takes a decimal number as input, stores the binary representation in an array, and then prints the hexadecimal output by mapping binary digits above 9 to A-F characters.

Uploaded by

abhiece
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<conio.h>
int i=0,bin[100];
void main()
{
int j=0;
int read(),destobin();
clrscr();
j=read();
destobin(j);
getch();
}
int read()
{
int num;
printf("\n PLEASE ENTER THE VALUE IN DECIMAL ( 0 - 15 ): ");
scanf("%d",&num);
return(num);
}
int destobin(int j)
{
for(i=0;j>0;i++)
{
bin[i]=j%16;
j=j/16;
}
i--;
printf("\nDECIMAL TO HEXADECIMAL CONVERSION IS \t");
for(;i>=j;i--)
{
if(bin[i]==10)
printf("A");
elseif(bin[i]==11)
printf("B");
elseif(bin[i]==12)
printf("C");
elseif(bin[i]==13)
printf("D");
elseif(bin[i]==14)
printf("E");
elseif(bin[i]==15)
printf("F");
else
printf("%d",bin[i]);
}
}
////////////////////////////////////////////////////////////////////////////////
/////
Here is a program that works using this method:
#include <stdio.h>
#include <conio.h>
int arrToInt(int nums[], int cnt);
int main()
{
int num;
int cnt = 0;
int nums[100];
printf("Enter the number in decimal: ");
scanf("%d", &num);
while (num != 0)
{
/* we save all reminders in the array */
nums[cnt++] = num % 8;
num = num / 8;
}
printf("Number in octal: %d\n", arrToInt(nums, cnt));
return 0;
getch();
}
int arrToInt(int nums[], int cnt)
{
int tmp = 0;
for (cnt = cnt - 1; cnt >= 0; cnt--)
{
tmp += nums[cnt] * pow(10, cnt);
}
return tmp;
}

Read more: http://wiki.answers.com/Q/What_is_a_C_program_to_convert_decimal_numb


er_to_octal_number#ixzz17SbCGoU0

You might also like