A.) Write a function named powfun () that raises an integer number passed to it to a positive integer power and displays the result. The positive integer should be the second value passed th the function. Declare the variable used to store the result as a long-integer data type to ensure sufficient storage for the result.
B.) Include the function written in (A.) in a working program. Make sure your function is called from main ().
Test the function by passing various data to it.
I need help to write this C++ code urgently. I don't know where to start??
just first try to learn c++ then code otherwise u will start hating it.
Reply:unsigned long powfun(int base, int power)
{
unsigned long res = 1;
for (int i=power; i%26gt;=1; --i)
res = res * base;
return res;
}
Now, do the main() function yourself.
Reply:A) create a function that returns long-integer
make sure the function has 2 parameters one for the base and one for the power.. example call power_func(base, power); once u have the skeleton done, write a loop the decreases Power until it is one, every time it decreases the power it should multiply the base time its self and store that into a temp. variable, then return That temp variable outside ur loop, after it
also u have to account for the 0th power which is always 1, u might want to block ur while with an if statement
Thats a normal way for doing powering , there are way faster algorithms out there for fast powering, like interval halving
Reply:#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;math.h%26gt;
int main()
{
int integer,
power,
result;
printf("Please input the integer\n");
scanf("%d",%26amp;integer);
printf("Please input the power\n");
scanf("%d",%26amp;power);
{
result=pow(integer,power);
}
printf("%d raised to the %d power is %d",integer,power,result);
system("PAUSE");
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment