Working on an assignment where we convert a decimal int into a binary number, back to a decimal, then apply it as a power to a base number.
Problem: When I plug numbers into bin it changes the value of base and power to something else. Like if my input is 5, it changes to 53. Some other tests were 7 changing to 55, 3 changing to 51, and 10 changing to 49. Any ideas why they're changing?
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int bin(int x, int y) {
int t = 0, adder = 1, output = 1, debug = y;
for (int i = 0; debug != 0; i++) {
if (debug % 2 != 0) {
for (int l = 0; l < i; l++) {
adder *= 2;
}
t += adder; //adder = 2^1
adder = 1;
}
debug /= 2;
}
for (int i = 0; i < t; i++) {
output *= (x - 48);
}
return output;
}
int main () {
int base, power, out, test;
printf("Enter a base:\n");
scanf("%ls", &base);
printf("Base: %ls\n", &base);
printf("Enter a power:\n");
scanf("%ls", &power);
printf("Power: %ls\n", &power);
out = bin(base, power);
printf("%d\n", out);
test = pow(5, 5);
printf("%d\n", test);
}