0

how to do (y = x^n) in C ?

for example y = 5^4 = 625

30th Nov 2020, 5:10 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
8 Respuestas
+ 4
To calculate the power of two numbers, you can either implement a function yourself, or use the standard pow() function included in the <math.h> header file: https://en.cppreference.com/w/c/numeric/math/pow
30th Nov 2020, 5:13 PM
Shadow
Shadow - avatar
+ 1
Then you need to multiplicate x for itself n times. Like 5*5*5*5 Just uss a for loop
30th Nov 2020, 6:22 PM
Davide
Davide - avatar
+ 1
Try this code #include <stdio.h> int main() { int base,power,i,result=1; printf("enter base and power:"); scanf("%d %d",&base,&power); for(i=1;i<=power;i++) { result=result*base; } printf("%dth power of %d is %d",power,base,result); return 0; }
1st Dec 2020, 4:14 AM
Sunil Kunwar
Sunil Kunwar - avatar
0
need to do it withou pow
30th Nov 2020, 5:14 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
how it could look ?i cant imagine
30th Nov 2020, 10:32 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
Just try something. If it works, congratulations. If it doesn't work you have two possibilities: 1) try something else 2) post here your attept so that someone can tell you why it doesn't work
30th Nov 2020, 10:39 PM
Davide
Davide - avatar
0
no ,i cant imagine how to write loop for just write if you can loop for
30th Nov 2020, 10:41 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
Then that's what you need: https://www.sololearn.com/learn/C/2927/
30th Nov 2020, 10:44 PM
Davide
Davide - avatar