Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Decimal to Binary
Author Message
alpesh

Posted: Sat Mar 10, 2007 7:18 am   Post subject: Decimal to Binary

Quote:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,r,s=0,i=0;
clrscr();
scanf("%d",&n);
while(n>0)
{
r=n%2;
s=s+r*pow(10,i);
n=n/2;
i=i+1;
}
printf("%d",s);
getch();
}

GiffordBruno

Posted: Tue May 25, 2010 8:32 am   Post subject: RE:Decimal to Binary

Very nice result. Thanks for this great information.
A.J

Posted: Tue May 25, 2010 10:36 am   Post subject: RE:Decimal to Binary

If you want, you could code you own 'bitset' function for C. Basically, bitset<x>(d) outputs 'd' into binary, showing 'x' bits (so if you output bitset<10>(5), it outputs 0000000101). Though the problem with that is it only converts to binary.

Good job though.
Prabhakar Ragde

Posted: Tue May 25, 2010 10:56 am   Post subject: RE:Decimal to Binary

Two possible improvements:

1) Eliminate the use of the `pow' function, which isn't needed.

2) Write a version which works for all integers n that can be represented as C ints. Currently this program only works for small nonnegative integers.
coolgod

Posted: Sat Aug 07, 2010 2:16 am   Post subject: Re: Decimal to Binary

a better one would be
Quote:

#include <stdio.h>

int main()
{
int n;
printf("Please input the integer you want to convert to binary:");
scanf("%d", &n);
int i;
for(i=0; i<bits; i++)
{

printf("%d", n<0);
n <<= 1;
}
printf("\n");
return 0;
}

 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: