GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 407 of 453    Print  
Find the binary form
Write a program to find the binary form for the given charcter input.
eg:- for 'A' ASCII value is 65 and its binary form is 1000001.



  
Total Answers and Comments: 9 Last Update: August 31, 2009     Asked by: abhijeetsingh 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: kotsiman
 
main()
{
  unsigned char a;
  unsigned char mask = 0x80;
  clrscr();
  puts("Enter any single charactern");
  scanf("%c",&a);
  puts("binary equvalent of your nuber is ");
  while(mask)
  {
      (a & mask)? printf("1"):printf("0");
      mask >>= 1;
  }
  getch();
}




Above answer was rated as good by the following members:
RoMBiN, lifeisbeautiful
April 03, 2008 02:58:48   #1  
selloorhari Member Since: April 2008   Contribution: 2    

Ans fro the QUE " Find the binary form"
#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 10

void main()
{
int d i j[MAX_SIZE] k MAX_SIZE - 1 l ;
char a;

clrscr();

for ( i 0 ; i < MAX_SIZE ; i++ )
{
j[i] 0 ;
}

printf("n Enter the Letter " ) ;
scanf ( " c" &a) ;

printf ( "n The Input is : c" a) ;

d (int) a;
printf ( " n The Decimal Format of c is : d " a d ) ;
i d ;


do
{
l i 2 ;
j[k] l ;
i i/2;
k-- ;
}while ( i ! 0 );

i 0 ;
l 0 ;
while ( j[i] 0 )
{
i++ ;
l++ ;
}
printf ( " n The Binary Format of c is : " a );
for ( i l ; i < MAX_SIZE ; i++ )
{
printf (" d" j[i]);
}

getch();

}

 
Is this answer useful? Yes | No
April 04, 2008 01:59:02   #2  
Nihar Shukla Member Since: April 2008   Contribution: 3    

RE: Find the binary form
// Here is a simple recursive solution

#include <iostream>

void binaryForm(int dividend)

{

int quotient;if(dividend 1)

{

printf(" d" dividend);

return;

}

else

{

quotient dividend 2;

dividend dividend/2;

binaryForm(dividend);

printf(" d" quotient);

}

}

int main()

{

char ch;int num;

printf("Enter a character :");

scanf(" c" &ch);

binaryForm(ch);

system("PAUSE");

return 0;

}


 
Is this answer useful? Yes | No
April 09, 2008 10:10:18   #3  
ashoksurati Member Since: April 2008   Contribution: 13    

RE: Find the binary form
This is one of the way to find the binary value of the given number.

#include
#include
main()
{
int no k p 1;
long int bin 0;
clrscr();
puts( Enter any number to find the binaryn );
scanf( d &no);
dno no;

while(n)
{
k no 2;
bin bin + p * k;
p p * 10;
no no / 2;
}
puts( ************************************n );
printf( dThe given number is : dno);
printf( l The binart format is : bin);
puts( n*********************************** );
}


Thank U

Ashok kumar
asho . 28 AT gmail . com

 
Is this answer useful? Yes | No
April 16, 2008 08:17:53   #4  
vjaya Member Since: December 2007   Contribution: 2    

RE: Find the binary form

//Another Method

int main()
{
int lBinaryValue 0;
char chInputChar;
printf("Enter a character");
scanf(" c" &chInputChar);
lBinaryValue ConvertDecimalToBinary((int)chInputChar);
printf(" d" lBinaryValue);
return 0;

}
int ConvertDecimalToBinary(int lInput)
{
int lTempInt lInput;
int lBinaryOutput 0;
int i 0;
while(lTempInt > 0)
{
lBinaryOutput lBinaryOutput +(lTempInt 2)*((int)pow(10 i++));
lTempInt lTempInt/2;
}
return lBinaryOutput;
}


 
Is this answer useful? Yes | No
May 20, 2008 03:08:09   #5  
aman15490 Member Since: May 2008   Contribution: 70    

RE: Find the binary form
/*a very simple solution*/
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int i k d 1 sum 0;
printf("enter the chracter");
scanf(" c" &ch);
i ch;
while(i> 1)
{
k i 2;
sum sum+k*d;
i i/2;
d d*10;
}
printf(" binary is d" sum);
getch();
}


 
Is this answer useful? Yes | No
May 22, 2008 13:26:31   #6  
jintojos Member Since: May 2008   Contribution: 29    

RE: Find the binary form

#include<conio.h>
#include<stdio.h>
void fun(int);
void main()
{
char ch;
clrscr();
printf("Enter The Char :");
scanf(" c" &ch);
printf("The Binary Of d Is :");
fun(ch);
getch();
}

void fun(int num)
{
int rem;
if(num 0) return;
rem num 2;
fun(num/2);
printf(" d" rem);
}


 
Is this answer useful? Yes | No
May 22, 2008 16:48:56   #7  
kernel32 Member Since: May 2008   Contribution: 6    

RE: Find the binary form
#include<stdio.h>

void recDec2Bin(int dec)
{
if(dec 1){
printf("1");
}else{
//printf(" d" dec 2); // to print it inverse
recDec2Bin(dec/2);
printf(" d" dec 2);
}
}

int main(int argc char *argv[])
{
if(argc > 2){
printf("Error: Number of argument");
}else{
recDec2Bin(atoi(argv[1]));
}
printf("n");
return 0;
}

 
Is this answer useful? Yes | No
June 21, 2008 23:52:08   #8  
kotsiman Member Since: June 2008   Contribution: 1    

RE: Find the binary form
main()
{
unsigned char a;
unsigned char mask 0x80;
clrscr();
puts("Enter any single charactern");
scanf(" c" &a);
puts("binary equvalent of your nuber is ");
while(mask)
{
(a & mask)? printf("1"):printf("0");
mask >> 1;
}
getch();
}



 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
August 31, 2009 09:50:14   #9  
elexpert_arka Member Since: August 2009   Contribution: 1    

RE: Find the binary form
Here is a simple code which will just get each bit of the char by AND operation


int main ()
{
char a; int i j;
printf("Enter a charn");
scanf(" c" &a);
printf("The binary value of c " a);
for (i 0 j 128; i<8; i++ j>> 1)
{
if ((a & j))
{
printf("1");
} else {
printf("0");
}
}
fflush(stdin);
getchar();
}

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape