Ok, so essentially I have to write a basic program in C-- where x^y outputs a re
ID: 440535 • Letter: O
Question
Ok, so essentially I have to write a basic program in C-- where x^y outputs a result (z) and I'm not really sure how to put it into an acceptable form that the computer can read I have all the information I think, but i just dont know how to put it together - So I create 3 variables - x,y and z (%x^y = z) - y HAS to be an integer, if y is not integer, then program says "Error, y needs to be integer" - z = x multiplied y-times (So if y = 2, then z = x*x, or if y = 3, then z = x*x*x) - If y = 0, then z = 1 - Else if y > 0 then z = x^y - Else if y < 0 then z = 1/x^y Also if I'm missing any possible scenario, do you mind telling me? I think I got them allExplanation / Answer
float power(){
int x,y,z,i=0;
scanf("%d%d",&x,&y);
if(IsInt(y)){
if(y>0)
for(i=0;i<y;i++)
z *= x ;
if(y==0)
z = 1 ;
if(y<0){
for(i=0;i<y;i++)
z *= x ;
z = 1/z ;
}
}
else{
printf("Error, y needs to be integer");
return 0 ;
}
return z ;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.