VB icon

A Program Convert Binary to Decimal Number ]

Email
Submitted on: 1/8/2015 3:55:00 PM
By: Cool_Gemini (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: C, C++ (general), UNIX C++
Views: 3807
 
     Write a program to convert binary number to decimal number.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: A Program Convert Binary to Decimal Number ]
// Description:Write a program to convert binary number to decimal number.
// By: Cool_Gemini (from psc cd)
//**************************************

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(void)
{
 int i=0,j=0,sum=0,n,tmp,tmp1;
 long int arr[5];
 char pch;
 clrscr();
 printf("Enter binary number : ");
 while (scanf("%d",&n) != 1)
			 {
				while ((pch = getchar()) != '\n')
					putchar(pch); // dispose of bad input
				 printf(" is not an binary number.\nPlease enter a ");
				 printf("binary value, such as 100, 1011, or 1111: ");
			 }
 while(n!=0)
	{
		arr[i]=n%10;
		n=(n-arr[i])/10;
		tmp=i;
		i++;
	}
 if(i>=5)
	{
		printf("\n\tNumber Out Of Range");
		getch();
		exit(0);
	}
 for(j=0;j<=tmp;j++)
	if(arr[j]!=1&&arr[j]!=0)
	{
		printf("Enter binary no. only");
		getch();
		exit(0);
	}
 tmp1=tmp;
 for(j=0;j<=tmp;j++)
	{
		sum=sum+(arr[j]*(pow(2,tmp1)));
		tmp1--;
	}
 printf("\nEquivalent decimal number is %d",sum);
getch();
return 0;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/2/2015 3:17:31 PMjenny

Converts Decimal Numbers to Binary

#include
#include
void main()


{



int i, ch;
cout<<"Enter an integer: ";
cin>>ch;
for(i=0x80;i;i=i>>1)
cout<<((ch&i)?'1':'0');


getch();

cout< }
Converts Decimal Numbers to Binary

#include
#include
void main()


{



int i, ch;
cout<<"Enter an integer: ";
cin>>ch;
for(i=0x80;i;i=i>>1)
cout<<((ch&i)?'1':'0');


getch();

cout< }
http://cplusplus.happycodings.com/beginners-lab-assignments/code41.html
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.