C program to find nth power of integer m without pow().
Input:
m=3 n=2
output:
9.000
Tests to validate the program works as expected!
- For negative M
Input : -2 3
output : -8.000
- For negative N
Input : 2 -3
output : 0.125000
- For negative M and N
Input : -2 -3
output : -0.125000
However I am not getting the desired output
void main()
{
signed int m, n;
int i;
float p;
clrscr();
printf("Enter the number and its power (exponent)
");
scanf("%d%d",&m,&n);
p=1;
if (n==0)
{
printf("%d raised to %d is: %f",m,n,p);
}
if (n>0)
{
for( i = 0 ; i < n ; i++ )
p*=m;
if(m>0)
printf("%d raised to %d is: %f",m,n,p);
if(m<0)
printf("%d raised to %d is: %f",m,n,-p);
}
if (n<0)
{
n=-n;
for( i = 0 ; i < n ; i++ )
p*=m;
if(m>0)
printf("%d raised to %d is: %f",m,-n,1/p);
if(m<0)
printf("%d raised to %d is: %f",m,-n,-(1/p));
}
getch();
}
Can u kindly provide the correct program for the test cases?
I can't declare signed float
as it is giving an error.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…