CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions OneStopTesting.com - Collection of Testing EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGate.com - Collection of Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - Collection of MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - Collection of IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - Collection of SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - Collection of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.

Sponsored Links

Interview Questions
Our Services

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 1757
Rate it!


Answers:

Source: CoolInterview.com

#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

Source: CoolInterview.com

#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

Source: CoolInterview.com

#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

Source: CoolInterview.com

#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

Source: CoolInterview.com

#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

Source: CoolInterview.com

#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

Source: CoolInterview.com

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

Source: CoolInterview.com

/*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

Source: CoolInterview.com

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

Source: CoolInterview.com

#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

Source: CoolInterview.com

#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

Source: CoolInterview.com

#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

Source: CoolInterview.com

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

Source: CoolInterview.com

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
    Recovery of bad debts written off previously will be? a) Credited to debtors A/c b) Adjusted against provision for doubtful debts c) Debited to debtors A/c d) Credited to Profit and Loss A/c
    A free market economy's driving force is: a)Profit motive b)Welfare of the people c)Rising income and levels of living d)None of the above
    Purchase of second- hand computer on credit by a cloth merchant will be recorded in: a) Journal b) Cash Book c) Purchase Book d) None of the above
    Proprietor's Account is _________ Account. a) Real b) Nominal c) Personal d) None of these
    Goods worth Rs. 10,000 were withdrawn by the proprietor for his personal use. The account to be credited is a) Sales A/c b) Drawings A/c c) Purchases A/c d) Expenses A/c
    Rs. 1,500 withdrawn for personal use should be debited to ____________ a) Expense Account b) Purchases Account c) Sales Account d) Drawings Account
    Unexpired expense is _________ account. a) Real b) Nominal c) Personal d) Representative Personal
    The rule 'Debit all expenses & losses and Credit all incomes & gains' relates to: a) Personal Account b) Real Account c) Nominal Account d) None of these
    The rule regarding PERSONAL ACCOUNT IS: a) Debit what comes in, credit what goes out b) Debit all expenses and losses, credit all incomes and gains c) Debit the receiver, credit the giver d) None
    General Reserve Account and Dividend Equalisation Fund Account are: a) Personal A/c b) Real A/c c) Nominal A/c d) None
    If two or more transactions of the same nature are journalized together it is known as: a) Compound journal entry b) Separate journal entry c) Posting d) None of the above
    Outstanding salary is a: a) Real account b) Personal account c) Representative personal account d) Nominal account
    Bank overdraft account is a: a) Personal account b) Real account c) Nominal account d) Representative personal account
    What is Luxury Tax in Taxation?
    What is Lump-sum Taxation in Taxation?
    What is Lump-sum Rate in Taxation?
    What is Lump-sum Exempt Amount in Taxation?
    What is Lump-sum Deductions in Taxation?
    What is Lottery Tax in Taxation?
    What is Loss Relief in Taxation?



    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 = 1.058594 Seconds