Conversion of Numbers in C

Advertisements

Prev Tutorial Next Tutorial

Number Conversion Program in C

With the help of C Language you can convert any number from decimal to binary and binary to decimal, binary to octal, binary to hexadecimal, decimal to octal and etc..

Decimal to Binary Conversion

Decimal number have base 10 and Binary number have base 2.

Decimal to Binary Conversion in C

#include<stdio.h>
#include<conio.h>

void main()
{
int no,rem[20],i=0,j;
clrscr();
printf("Enter any number: ");
scanf("%d",&no);
while(no>0)
{
rem[i]=no%2;
i++;
no=no/2;
}
printf("Binary number is: ");
for(j=i-1;j>=0;j--)
{
printf(" %d",rem[j]);
}
getch();
}

Output

Enter any number: 7
Binary number is: 111

Decimal to octal conversion

Decimal number have base 10 and Octal number have base 8.

Decimal to Octal Conversion Program in C

#include<stdio.h>
#include<conio.h>

void main()
{
int no,rem[20],i=0,j;
clrscr();
printf("Enter any number: ");
scanf("%d",&no);
while(no>0)
{
rem[i]=no%8;
i++;
no=no/8;
}
printf("Octal number is:");
for(j=i-1; j>=0; j--)
{
printf("  %d",rem[j]);
}
getch();
}

Output

Enter any number: 23
Octal number is: 2  7

Decimal to Hexadecimal Conversion

Decimal number have base 10 and Hexadecimal number have base 16.

Decimal to Hexa Conversion Program in C

#include<stdio.h>
#include<conio.h>

void main()
{
int num,rem[20],hex=0,i=0,j;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
while(num>0)
  {
    rem[i] = num % 16;
    num = num / 16;
    i++;
  }
printf("Hexadecimal number: ");
for(j=i-1; j>=0 ; j--)
  {
    switch(rem[j])
    {
      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("%d",rem[j]);
    }
  }
getch();
}

Output

Enter any number: 231
Hexadecimal number: E7

Prev Tutorial Next Tutorial

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Advertisements


Buy Websites 9999595223

Buy Fresh and Payment Receive Media.net Account with Website. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs