•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,921 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,688 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
This snippet has a will convert Decimal number to Binary number.
// coverts decimal to binary // vishesh // 21/05/2005 #include <iostream> char* DEC_BIN(int); int main() { // int num; std::cout << "Enter a number: "; std::cin >> num; std::cout << DEC_BIN(num); std::cin.get(); return 0; } char* DEC_BIN(int dec) { using namespace std; char *rtn; int prev, len, temp, *bin; prev = dec; for(int i=0;prev!=0;i++) { prev = prev/2; len=i; } bin = new int[len+1]; prev = dec; for(int i=0;prev!=0;i++) { bin[i] = prev%2; prev = prev/2; len=i; } bin[len+1] = prev%2; len++; for(int i=0;i<len/2;i++) { temp=bin[i]; bin[i]=bin[len-1-i]; bin[len-1-i]=temp; } rtn = new char[len+1]; for(int i=0;i<len;i++) { rtn[i]=bin[i]+'0'; rtn[i+1]='\0'; } return rtn; }
Comments (Newest First)
hashjoe | Newbie Poster | Jun 9th, 2008
•
•
•
•
na na na lot of work :
works in C pretty well :
#include<stdio.h>
main()
{
int i=0,q,num,r[20];
printf("\n\n\nEnter Number\n\n\n");
scanf("%d",&num);
q = num/2;
r[0] = num%2;
while (q>1)
{
q = q/2;
r[i+1] = q%2;
printf("\n\n\n\nbinary is %s\n\n\n",r);
++i;
}
getch();
}
works in C pretty well :
#include<stdio.h>
main()
{
int i=0,q,num,r[20];
printf("\n\n\nEnter Number\n\n\n");
scanf("%d",&num);
q = num/2;
r[0] = num%2;
while (q>1)
{
q = q/2;
r[i+1] = q%2;
printf("\n\n\n\nbinary is %s\n\n\n",r);
++i;
}
getch();
}
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)