1
I’m starting to learn how to program, so forgive me any more silly mistakes. It’s a simple exercise, doing a C power by setting the base and exponential. Here’s my code:
#include <stdio.h>
int main () {
int n = 0;
float x = 0;
int k = 1;
float s = 0;
scanf("%d", &n);
scanf("%f", &x);
do{
if(k!=n){
k = k + 1;
s = x*x + s;
}
else { s = x;
}
} while( k != n);
printf("%f", s);
return 0;
}
Question is that for some reason, from the second operation it multiplies the X as an integer, example:
2.1 3: He should give me 2.1*2.1*2.1 = 9.26
Instead he gives me 2.1*2.1*2 = 8.82
Does anyone have any idea why this is and how to fix it?
I believe your algorithm is wrong. You are making a sum of x 2.
– anonimo
An alternative is:
float s=1;
and replace your loop with:for (k=1; k<=n; k++) s *= x;
.– anonimo
why not use Pow?
– lemoce