I started to learn C a week ago and this is my culminating program of what I've learned but I could use a few pointers.
#include <math.h>
#include <stdio.h>
void binary(int n){
int x = 0, m = 0, z = n;
while(n >= 1){
++x;
n /= 10;
}
n = z;
int i[x];
x = 0;
while (n != 0){
++x;
i[x] = n % 10;
n /= 10;
}
while(x >= 0){
if (i[x] == 1){
if (x == 1)
++m;
else m += pow(2,x - 1);
}
--x;
}
printf("%d", m);
}
void convert(int n){
int i[16];
int j, k, p;
for(k = 15; k >= 0; --k){
i[k] = n % 2;
n /= 2;
}
int *a, *b;
a = &i[0];
b = &i[16];
for( ;*a == 0; ++a)
;
while(a != b){
printf("%d", *a);
++a;
}
}
int main()
{
int n;
char m;
printf("\nType b for binary to decimal conversion or d for decimal to binary conversion (max 65535)\n");
m = getchar();
printf("\nPrint number to be converted\n");
scanf("%d", &n);
if (m == 'b'){
printf("%d converted to a decimal number is ", n);
binary(n);
}
else{
printf("%d converted to a binary number is ", n);
convert(n);
}
}