Without using math.h; Write two functions: int doPower(int base, int exp); int f
ID: 3621662 • Letter: W
Question
Without using math.h; Write two functions:int doPower(int base, int exp);
int findMaximum(int a, int b);
The first returns base raised to the power exp, which you can assume is nonnegative, and the second, the maximum of a and b.
main inputs two nonnegative integers x and y. Then uses doPower to evaluate x^y and y^x, and findMaximum to find the larger among them, printing all 3.
(~)$ a.out
Enter x and y: 2 3
doPower(2 3)=8
doPower(3 2)=9
max=9
(~)$ a.out
Enter x and y: 2 0
doPower(2 0)=1
doPower(0 2)=0
max=1
(~)$ a.out
Enter x and y: 0 0
doPower(0 0)=1
doPower(0 0)=1
max=1
(~)$
Explanation / Answer
please rate - thanks
#include<stdio.h>
#include<conio.h>
int doPower(int base, int exp);
int findMaximum(int a, int b);
int main()
{
int x,y,max,w,z;
printf("Enter x and y: ");
scanf("%d",&x);
scanf("%d",&y);
w=doPower(x,y);
z=doPower(y,x);
printf("doPower(%d %d)=%d ",x,y,w);
printf("doPower(%d %d)=%d ",y,x,z);
printf("max=%d ",findMaximum(w,z));
getch();
return 0;
}
int doPower(int base, int exp)
{int ans;
int i;
if(exp<0)
return 0;
else
if(exp==0)
return 1;
else
{ans=base;
for(i=2;i<=exp;i++)
ans=ans*base;
return ans;
}
}
int findMaximum(int a, int b)
{if(a>b)
return a;
return b;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.