# Help with pow()

• 10-09-2006
jedi_jinn
Help with pow()
Code:

#include <stdio.h>
#include <math.h>

int main()
{
int base, exp;
scanf("%d", &base) ;
scanf("%d", &exp) ;
printf("%d ^ %d = %d\n", base, exp, pow(base,exp)) ;
return 0;
}

The above code prints the output of pow as a 0. What did I do wrong? I tried %f also, but that gave me a number with alot of zeroes after the decimal. How can I get a normal integer without casting pow? Thank you.
• 10-09-2006
JDD
Quote:

Originally Posted by jedi_jinn
Code:

#include <stdio.h>
#include <math.h>

int main()
{
int base, exp;
scanf("%d", &base) ;
scanf("%d", &exp) ;
printf("%d ^ %d = %d\n", base, exp, pow(base,exp)) ;
return 0;
}

The above code prints the output of pow as a 0. What did I do wrong? I tried %f also, but that gave me a number with alot of zeroes after the decimal. How can I get a normal integer without casting pow? Thank you.

Code:

printf("%i ^ %i = %i\n" , base, exp, pow(base, exp));
Try that.
Or, you can always error check.

Code:

int main()
{
int base, exp, powr;
scanf("%d", &base) ;
scanf("%d", &exp) ;

powr = pow(base, exp);

if(powr != 0)
{
printf("%i ^ %i = %i\n", base, exp, powr) ;
}
else
{
printf("Error calculating pow()");
return 0;
}

• 10-09-2006
Dave_Sinkula
Quote:

Originally Posted by jedi_jinn
What did I do wrong?

^ is an exlusive or.
• 10-09-2006
JDD
Quote:

Originally Posted by Dave_Sinkula
^ is an exlusive or.

Oh thats right, the only way i can think of printing it, would be to...

Code:

int main()
{
int base, exp;
double powr;
char expc;
scanf("%i", &base) ;
scanf("%i", &exp) ;

powr = pow(base, exp);

if(powr != 0)
{
printf("%i %c %i = %d\n", base, expc, exp, powr);
}
else
{
printf("Error calculating pow()");
return 0;
}

• 10-09-2006
Dave_Sinkula
What is the return value of pow? [Hint: it's not an int, with which %d corresponds.] Have you ever looked up what %g does?
• 10-09-2006
JDD
Quote:

Originally Posted by Dave_Sinkula
What is the return value of pow? [Hint: it's not an int, with which %d corresponds.] Have you ever looked up what %g does?

It returns a double doesn't it? ;x
• 10-09-2006
jedi_jinn
Oh thanks for the responses
Do you know where I can find information on format descriptors like %d, %g, %e, %f?
• 10-09-2006
jafet