Skip to content

Instantly share code, notes, and snippets.

@putheakhem
Created February 9, 2017 16:02
Show Gist options
  • Save putheakhem/bdfbb9ed1f39cbf75fe071c381e3c13e to your computer and use it in GitHub Desktop.
Save putheakhem/bdfbb9ed1f39cbf75fe071c381e3c13e to your computer and use it in GitHub Desktop.
This Program allow user to convert an integer in decimal value into base 2, 8, 16
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long decimalToBinary( long integerInDecimal);
long decimalToOctal( long integerInDecimal);
void decimalToHexadecimal( long integerInDecimal);
int main() {
long integerInDecimal;
char option = 'y';
// Display information
printf("===============================================================================\n");
printf("This Program allow user to convert an integer in decimal value into base 2, 8 , 16\n");
printf("Author Name: Khem Puthea\n");
printf("===============================================================================\n");
/* check condition to restart or quit */
while (option == 'y') {
printf("Enter a decimal number: ");
scanf("%ld", &integerInDecimal);
printf("Octal number of %ld is %ld\n", integerInDecimal, decimalToOctal(integerInDecimal));
printf("Binary number of %ld is %ld\n", integerInDecimal, decimalToBinary(integerInDecimal));
/* Directly call function convert decimal to hexadecimal*/
printf("Hexdecimal number of %ld is ", integerInDecimal);
decimalToHexadecimal(integerInDecimal);
/* ask user to choose option restart or quit*/
printf("\nDo you want to continue: (y/n) ");
scanf("%s", &option);
printf("----------------------------------------------------------\n");
}
printf("Thank you for using this conversion application\n");
printf("=================================================");
return 0;
}
/* Function to convert a decinal number to octal number */
long decimalToOctal(long integerInDecimal) {
int remainder;
long octal = 0, i = 1;
while(integerInDecimal != 0) {
remainder = integerInDecimal%8;
integerInDecimal = integerInDecimal/8;
octal = octal + (remainder*i);
i = i*10;
}
return octal;
}
/* Function to convert a decinal number to binary number */
long decimalToBinary(long integerInDecimal) {
int remainder;
long binary = 0, i = 1;
while(integerInDecimal != 0) {
remainder = integerInDecimal%2;
integerInDecimal = integerInDecimal/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}
/* Function to convert a decimal number to Hexadecimal*/
void decimalToHexadecimal( long integerInDecimal) {
long int remainder[50],i=0,length=0;
while(integerInDecimal>0) {
remainder[i]= integerInDecimal%16;
integerInDecimal = integerInDecimal/16;
i++;
length++;
}
printf(" : ");
for(i=length-1;i>=0;i--) {
switch(remainder[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",remainder[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment