64 538
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #39395 in C++ for Theodore

Question #39395
I am new to C++ and have been trying to understand the function below, I need a step by step of what it is actually doing, its a function that takes a BASE and a power it works but wish to know how exactly it works. I can do base calculation off the top of my head but this is a little different.

#include<stdio.h>
int pw(int,int);
int main()
{
int x,n,ans;
printf("Enter the numbers:base and power\n");
scanf("%d%d",&x,&n);
ans=pw(x,n);
printf("The ans is: %d\n",ans);
return 0;
}


int pw(int x,int n)
{
int i,y=1;
if(n==0)
return 0;
else if(n==1)
return x;
else if(n==2)
return(x*x);
else
{
y=x*x;
for(i=3; i<=n; i++)
{
y=y*x;
}
return (y);
}
}
Expert's answer

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions