c conversion program

      Comments Off on c conversion program

c conversion program

c conversion program

c conversion program is collection of programs for converting number to another type of number for example conversion from binary to decimal, binary to octal, binary to hexa, decimal to binary, decimal to hexa, decimal to octal and so on. I have tried to use simple logic so you can able to remember easily with little practice.

c conversion program : Binary to decimal conversion

//Written by Adarsh Patel
// downloaded from http://adarshspatel.in/
// Binary to Decimal

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

void main()
{
	clrscr();
	long int a,i=0,temp,digit;
	long float no,sum=0.0;

	printf("Enter the binary number :");
	scanf("%lf",&no);

	temp=int(no);

	while(temp>0)
	{
		digit=temp%10;
		sum=sum+(pow(2,i)*digit);
		temp=temp/10;
		i++;
	}
	no=no-int(no);
	digit=0;

	i=-1;

//	while(i>=-3)
	while(no>0)
	{
		no=no*10;
		digit=no;
		sum=sum+(pow(2,i)*digit);
		no=no-digit;
		i--;
	}

	printf("\n Decimal Number is : %lf",sum);
	getch();
}

 

c conversion program : Binary to octal conversion

//Written by Adarsh Patel
// downloaded from http://adarshspatel.in/
// Binary to Octal

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

void main()
{
	clrscr();
	int a,i=0,j=0,digit,subdigit;
	long int temp;
	long float no,sum=0.0;
	char str[50]={NULL};

	printf("Enter the binary number :");
	scanf("%lf",&no);

	temp=no;

	while(temp>0)
	{
		digit=temp%1000;
		i=0;
		while(digit>0)
		{
			subdigit=digit%10;
			sum=sum+(pow(2,i)*subdigit);
			i++;
			digit=digit/10;
		}
		str[j++]=sum;
		sum=0.0;
		temp=temp/1000;
	}
	j--;

	do
	{
		printf("%d",str[j]);
		j--;

	}while(j>-1);

						//   Decimal Portion
	printf(".");
	no=no-int(no);
	digit=0;
	j=1;

	while(j<=5)
	{
		no=no*1000;
		digit=no;
		i=0;
		while(digit>0)
		{
			subdigit=digit%10;
			sum=sum+(pow(2,i)*subdigit);
			i++;
			digit=digit/10;
		}
		digit=sum;
		printf("%d",digit);
		sum=0.0;
		digit=no;
		no=no-digit;
		j++;
	}
	getch();
}

 

c conversion program : Binary to hexa conversion

//Written by Adarsh Patel
// downloaded from http://adarshspatel.in/
// Binary to Hexa decimal

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

void main()
{
	clrscr();
	int a,i=0,j=0,digit,subdigit;
	long int temp;
	long float no,sum=0.0;
	char str[50]={NULL};

				// user input

	printf("Enter the binary number :");
	scanf("%lf",&no);

				// find hexa and store to array
	temp=no;
	while(temp>0)
	{
		digit=temp%10000;
		i=0;
		while(digit>0)
		{
			subdigit=digit%10;
			sum=sum+(pow(2,i)*subdigit);
			i++;
			digit=digit/10;
		}
		str[j++]=sum;
		sum=0.0;
		temp=temp/10000;
	}

			// print to screen whatever store in array
	j--;
	do
	{
		if(str[j]>9)
			printf("%c",str[j]+55);
		else
			printf("%d",str[j]);
		j--;
	}while(j>-1);

				// Decimal Portion

	no = no - int(no);

	printf(".");
	digit=0;
	j=1;

	while(j<=2)
	{
		no=no*10000;
		digit=no;
		i=0;
		while(digit>0)
		{
			subdigit=digit%10;
			sum=sum+(pow(2,i)*subdigit);
			i++;
			digit=digit/10;
		}
		digit=sum;

		if(digit>9)
			printf("%c",digit+55);
		else if(digit>9 && digit<16)
			printf("%d",digit);
		else
			printf("%d",0);

		sum=0.0;
		digit=no;
		no=no-digit;
		j++;
	}

	getch();

}

 

c conversion program : Download all c programs

[sociallocker]Download[/sociallocker]