Sponsored Links

Interview Questions
Recommended Books












INTERVIEW QUESTIONS C FUNCTIONS IN C DETAILS
Question :

Write a Program to convert decimal to binary no.

Posted by: sait on 1/13/2013


Contact sait Contact sait

Category Functions in C Interview Questions
Rating (3.1) By 38 users
Added on 1/13/2013
Views 1771
Rate it!

Highly Recommended, Hand Picked Functions in C Books



Question :

Write a Program to convert decimal to binary no.


Answer:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main(int argc, char *argv[])
{
double num;
long dec;
double frac;
char decstr[1024], *decptr, fracstr[1024], *fracptr, *fracendptr;
printf("
Please enter a decimal number: ");
scanf("%lf", &num);
dec = (long)((long)num);
frac = num - (double)dec;

bzero(decstr, sizeof(decstr)); decptr = decstr + sizeof(decstr) - 1;
while(dec > 0 && decptr > decstr)
{
decptr--;
if(dec%2)
*decptr = '1';
else
*decptr = '0';
dec = dec/2;
}
if(decptr <= decstr)
{
printf("Oops...Buffer offerflow prevented. Terminating.");
exit(-1);
}
bzero(fracstr, sizeof(fracstr)); fracptr = fracstr; fracendptr = fracstr + sizeof(fracstr) - 1;
while(frac > 0 && fracptr < fracendptr)
{
frac = frac*2;
if((int)(frac))
*fracptr = '1';
else
*fracptr = '0';
frac = frac - (double)((int)frac);
fracptr++;
}
if(fracptr >= fracendptr)
{
printf("Oops...Buffer offerflow prevented. The fractional part may not be exactly accurate. But its pretty close.
");
}
printf("
Binary: %s.%s", decptr, fracstr);
return 0;
}

Source: CoolInterview.com



Posted by: Akhilesh Shirbhate

Contact Akhilesh Shirbhate Contact Akhilesh Shirbhate

#include<stdio.h>
void bin(int);
int main()
{int n;
printf("enter the decimal number you want to convert into binary");
scanf("%d",&n);
bin(n);
}

void bin(int n)
{ int k;
if(n<=1)
printf("%d",n);
else
{
k=n;
bin(n/2);
printf("%d",k%2);
}
}
Source: CoolInterview.com



Posted by: P.Karthikeyan

Contact P.Karthikeyan Contact P.Karthikeyan

#include<stdio.h>
main()
{
int i,j,n,b[20];
clrscr();
printf("enter the decimal number");
scanf("%d",n);
for(i=0;i<n;i++)
{
b[i]=n%2;
n=n/2;
}
for(j=n;j>0;j--)
{
printf("%d",b[j]);
}
getch();
}
Source: CoolInterview.com



Posted by: madhukar

Contact madhukar Contact madhukar

#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,ans=0;
printf("enter value to convert in binary");
scanf("%d",&a);
while(a!=0)
{
b=a%2;
a=a/2;
ans=ans*10+b;
}
printf("ans is %d",ans);
getch();
}


we can use long for big number
Source: CoolInterview.com



Posted by: rakesh chaudhary

Contact rakesh chaudhary Contact rakesh chaudhary

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

void main()
{
int num,i,binary[20];
clrscr();

printf("
Enter a number:: ");
scanf("%d",&num);

for(i=0;i<16;i++)
{
binary[i]= (num>>(15-i) & 1)? 1 : 0;
}

printf("
Binary number is
");

for(i=0;i<16;i++)
printf("%d",binary[i]);

getch();
}
Source: CoolInterview.com



Posted by: Chethan

Contact Chethan Contact Chethan

#include<stdio.h>
void main()
{
int num,i,f,size;
int k;
int count=0;
int ass=15;

int arr[16];
printf("Enter your number : ");
scanf("%d",&num);
for(i=num;i>=1;i/=2)
{
f = i%2 ;
printf(" %d",f);
for(k=0;k<=ass;k++)
{
arr[k] = f;
}
ass = ass - 1;
count = count + 1;
size = 15-count;
for(k =0;k<=size;k++)
arr[k] = 0;

}
printf("
");
printf("
for a 16bit compiler
");

for(k=0;k<=15;k++)
printf(" %d ",arr[k]);


}
Source: CoolInterview.com



Posted by: pinak mishra

Contact pinak mishra Contact pinak mishra

let the no is n the the code for it is
int k=1,s=0,d;
while(n>0)
{
d=n%2;
s=s+d*k;
n=n/2;
k=k*10;
}
printf("%d",s);

s contains the binary equivalent of entered decimal no.
Source: CoolInterview.com



Posted by: malaram kumhar

Contact malaram kumhar Contact malaram kumhar

/*decimal to binary conversion*/
#include<stdio.h>
main()
{
long a,d,bin=0,b=1;
int rem;
printf("enter a decimel integer
");
scanf("%ld",&a);
d=a;
while(a>0)
{
rem=a%2;
bin=bin+rem*b;
a=a/2;
b=b*10;
}
printf("decimal integer number is=%ld
",d);
printf("binary equivalent is=%ld
",bin);
}
Source: CoolInterview.com



Posted by: AMBIKA.R.BIRADAR

Contact AMBIKA.R.BIRADAR Contact AMBIKA.R.BIRADAR

void showbits(int n)
{
int i = 0;
while(i < (sizeof(n) * 8))
{
printf("%d ",(n & ~(~0u>>1) ? 1 : 0));
n = n<<1;
i++;
}
}
Source: CoolInterview.com



Posted by: sunil

Contact sunil Contact sunil

#include <stdio.h>
int main()
{
unsigned int num = 12345;
unsigned int count = 1;

count = count << ((sizeof(int)*8) - 1);

printf("input: %d
output: ", num);

while (count)
{
if (num & count)
{
printf("1");
}
else
{
printf("0");
}
count = count >> 1;
}
printf ("
");

return 0;
}


sample output
==============
[aneebk@localhost interview]$ cc to_convert_dec_to_binary.c
[aneebk@localhost interview]$ ./a.out
input: 12345
output: 00000000000000000011000000111001
[aneebk@localhost interview]$

Source: CoolInterview.com



Posted by: Aneeb

Contact Aneeb Contact Aneeb

#include<stdio.h>
#include<conio.h>
bin(int no);
void main()
{
int no;
clrscr();
printf("enter decimal no
");
scanf("%d",&no);
bin(no);
getch();
}
bin(int no)
{
int a;
if(no==0)
{
return(1);
}
else
{
a=no%2;
no=no/2;
bin(no);
}
printf("%d",a);
}
Source: CoolInterview.com



Posted by: Neha Vora

Contact Neha Vora Contact Neha Vora

#include<stdio.h>
int a = 32;

for(i=0;i< (sizeof(a)*8) ; i++ ) {
printf("%d",(a<<i & 1<<31)?1:0);
}
return 0;
}

Source: CoolInterview.com



Posted by: prashanjit ghosh

Contact prashanjit ghosh Contact prashanjit ghosh

bits_number(int n)
{
int i,k;
for(i=15;i>=0;i--)
{
mask=1<<i;
k=n&mask;
k==0?printf("0"):printf("1");
}
}

Source: CoolInterview.com



Posted by: MS

Contact MS Contact MS

void main()
{
int n[20],x,i=0,rem,a,num=0;

printf("Enter The NUMBEr: ");
scanf("%d",&num);
while(num!=0)
{
rem=num%2;
num=num/2;
n[i]=rem;
i++;
a=i;
}
printf("
BINARY NO = ");
for(i=a-1;i>=0;i--)
{
printf("%d",n[i]);
}
}
Source: CoolInterview.com



Posted by: Rahul agrawal,Dhule

Contact Rahul agrawal,Dhule Contact Rahul agrawal,Dhule

If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification Enter the above shown code: *
Inform me about updated answers to this question

Related Questions
View Answer
What is the output of
void main()
{
int a = (1,2,3);
printf("%d",a);
}

with reason.
}

- Functions in C Interview Questions & Answers"> View Answer
code To draw a three dimensional graph using c graphics
View Answer
What is meant by malloc function
View Answer
How do we get Square root of any number Without using sqrt() function?

View Answer
What are returned by printf(), scanf() functions,if they return anything means what are that?

View Answer
Have you heard of "mutable" keyword?


- Functions in C Interview Questions & Answers"> View Answer
What is the purpose of main( ) function?

View Answer
Is using exit() the same as using return?


View Answer
Is it possible to execute code even after the program exits the main() function?


View Answer
What is a static function?


View Answer
Difference between Funtion to pointer and pointer to function

View Answer
How can send unlimited no of arguments to a function, eg printf function can take any no of arguments

View Answer
We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()

View Answer
How argc and argv works in the following main function?


main(int argc,char *argv[])
{ int n,i=0;
while(argv[1][i]!='
View Answer
How to write a program such that it will delete itself after exectution?


View Answer
output of the following program
void main()
{
unsigned i;
i=100*400;
printf(

View Answer
When function say abc() calls another function say xyz(), what happens in stack?

View Answer
In c , main() is a function . and where is defined main() in c. bcz every function has three parts.
1>. decleration
2>. definition.
3>. calling

View Answer
How to write a C program to find the power of 2 in a normal way and in single step?

View Answer
What is the difference between goto and longjmp() and setjmp()?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View ALL Functions in C Interview Questions & Answers



User Options
India News Network

Latest 20 Questions
What are the Hadoop related Projects at Apache?
What are the modules included in Apache Hadoop Project?
What is Apache Hadoop?
What is a Private Cloud? Do we need one to handle Big Data?
How Can We Cope with the Impact of Big Data?
What skills do Big Data analysts need to have?
What does a Big Data analyst do?
What are some real-world examples of Big Data and how it is used?
What is Big Data Analytics?
Why Big Data is becoming so important?
What is the impact of Big Data on Sports Industry?
What is the impact of Big Data on Real estate Industry?
What is the impact of Big Data on Retail Banking Industry?
What is the impact of Big Data on Retail Industry?
What is the impact of Big Data on Technology Industry?
What is the impact of Big Data on Information Technology Industry?
What is the impact of Big Data on Internet of Things (IoT) Industry?
What is the impact of Big Data on Media Industry?
What is the impact of Big Data on Education Industry?
What is the impact of Big Data on Healthcare Industry?



Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |




Copyright ©2003-2016 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions



Cache = 0.742188 Seconds