I made a code in c that converts decimal to binary and viceversa, the problem i have is that when i do the convertion from decimal to binary it prints the numbers in the wrong order, for example, if i input the number 123 as a decimal it should print 11110110 but my program converts it as 10111110
here is the code:
#include <stdio.h>
#include <stdlib.h>
main()
{
int i, no, coc, re, opt;
int dec;
int bin, p, c;
printf ("\n1. Decimal to binary \n2. binary to decimal \n choose an option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
printf("\nDecimal No.: ");
scanf("%d",&no);
if (no>2)
{
printf("Your binary No.: ");
for (i=1;1<=no; i++)
{
coc=no/2;
no=coc;
re=coc%2;
printf("%d", re);
}
}
break;
case 2: printf("\nBinary No.: ");
scanf("%d",&bin);
c=1;
dec=0;
while(bin>0)
{
p=0;
p=c*(bin%10);
dec+=p;
c*=2;
bin/=10;
}
printf("Your decimal No.: %d",dec);
break;
default:
printf("\a\n\nError");
break;
}
return 0;
}