I need to multiply a number by 10 to the (x) power, depending on the exponent I may require.
I know there is a function, in the <math.h>
library, but I was wondering if I could make my own function to achieve basically the same but only for 10
, not any number. It´s for course homework, but since we haven´t been told about this library, I want to try to achieve it without said power()
function.
Here's my code; it does compile, but I get some weird number instead of the intended 5000
.
#include <cs50.h>
#include <stdio.h>
int ten_to_the(int n);
int main(void) {
int x = 50;
x *= ten_to_the(2);
printf("%.i\n", x);
}
int ten_to_the(int n) {
n = 1;
for (int i = 0; i < n; i++) {
n *= 10;
}
return n;
}