Home › Forums › C Programming › convert decimal to binary
- This topic has 1 reply, 2 voices, and was last updated 15 years, 4 months ago by GWILouisaxwzkla.
Viewing 1 reply thread
- AuthorPosts
- August 8, 2009 at 12:39 pm #2219saqibParticipant
i have written a programme to convert decimal to binary….if i giv input as 25, i am getting 791 as output………i am not able to find the error…tried alot….please check the code n say where is d error…n giv me a solution…….
CODE
#include#include
void main()
{
int n,j,k,c=0;
int m=0;
int a[20];
printf(“enter the decimal no: “);
scanf(“%d”,&n);
while(n>0)
{L:
j=n%2;
k=n/2;
n=k;
c++;
if(m!=c)
{
a[m]=j;
m++;
}
if(k!=0)
{
goto L;
}
else
{
break;
}
}
printf(“the equivalent binary no is “%d””,a[m]);
}
getch();
} - August 8, 2009 at 7:07 pm #3600GWILouisaxwzklaParticipant
could try this:
1234567891011121314151617181920212223<br />#include <br /><br />void main()<br />{<br /><br />int i;<br />cout << "Enter a number to convert ";<br />cin >> i;<br />int binaryArray [ 20 ] ; //whatever<br />int j = 0;<br />while ( i )<br />{<br />binaryArray [ j ++ ] = i % 2 ;<br />i /= 2;<br />}<br /><br />cout << endl << "Binary Version: " ;<br />while ( j -- > 0 )<br />cout << binaryArray [ j ];<br />cout << endl;<br />}<br />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.