calculate the power of 2 integers. For example, power(2,3)=8,
power(3,2)=9,po wer(4,0)=1. Not allow use power math function.Two
integers are read from input by scanf. The program must produce an
error message when the user enter a negative number, able to calculate
the powers until terminate by user "by entering ctl-Z". output show
both input integer and the result. program 1 use "while" statement to
do the calculation.pro gram 2 use "do-while"statement to do the
calculation. program 3 use "for"statem ent to do the calculation.
The following is what I did for the "for" statement. It's not working.
I also missed the part of showing an error message when the user enter
a negative number
#include "stdio.h"
int main()
{
int a,b,loop,result ;
if ((scanf("%d%d", &a,&b)==EOF) )
{
printf("\n Terminate by user\n");
break;
}
else
printf("Enter the base and power:\n");
scanf("%d%d",&a ,&b);
for (loop=1; loop<=b; loop++)
{
result=result*a ;
}
printf(" Base:",a);
printf(" power:",b);
printf(" result:",result );
return 0;
}
power(3,2)=9,po wer(4,0)=1. Not allow use power math function.Two
integers are read from input by scanf. The program must produce an
error message when the user enter a negative number, able to calculate
the powers until terminate by user "by entering ctl-Z". output show
both input integer and the result. program 1 use "while" statement to
do the calculation.pro gram 2 use "do-while"statement to do the
calculation. program 3 use "for"statem ent to do the calculation.
The following is what I did for the "for" statement. It's not working.
I also missed the part of showing an error message when the user enter
a negative number
#include "stdio.h"
int main()
{
int a,b,loop,result ;
if ((scanf("%d%d", &a,&b)==EOF) )
{
printf("\n Terminate by user\n");
break;
}
else
printf("Enter the base and power:\n");
scanf("%d%d",&a ,&b);
for (loop=1; loop<=b; loop++)
{
result=result*a ;
}
printf(" Base:",a);
printf(" power:",b);
printf(" result:",result );
return 0;
}
Comment