Alright im trying to write a fairly simple c program that will accept input from the user for both a number and a power to raise it to then display the result, it works up to the input part but the result is always some crazy number, im looking for any insight or methods to solve this solution, and please dont be vague because my instructor couldn't even find my error.

``````#include <stdio.h>
#include <stdlib.h>
/* homework 5.19 Author: Jeremy Villanueva */
/*begin Main Function */
int main(void)
{
/*initialize integers */
int pow;
int num;
long int res;
long int power(int,int);

/* enter the number and the power */
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d", &pow);
res= power(num,pow);
/*solution */
printf("\n%d to the power %d is: %ld\n",num,pow,res);

system("PAUSE");
return 0;
}

int i=1;
long int sum=1;
long int power(int num,int pow){
if(i<=pow){
sum=sum*num;
for ( i = 1; i < pow; i++);

}
return (power);

} /* end main function */``````

output

Enter a number: 3

Enter power: 5

3 to the power 5 is: 4199233
Press any key to continue . . .

## All 3 Replies

Wow your instructor couldn't find the error..Try this function

``````long int power(int n,int p)
{
long int ans = n;
int i = 1;

for (; i < p; ++i)
ans *= n;

return ans;
}``````
``````#include <stdio.h>
#include <stdlib.h>
/* homework 5.19 Author: Jeremy Villanueva */
/*begin Main Function */
int main(void)
{
/*initialize integers */
int pow;
int num;
long int res;
long int power(int,int);

/* enter the number and the power */
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d", &pow);
res= power(num,pow);
/*solution */
printf("\n%d to the power %d is: %ld\n",num,pow,res);

system("PAUSE");
return 0;
}
//Did it Compiled???
int i=1;
long int sum=1;
long int power(int num,int pow){
if(i<=pow){
sum=sum*num;
for ( i = 1; i < pow; i++);

}
return (power);

} /* end main function */``````

gerard you got it exactly, thank you very much this has been annoying me for quite some time, saad yours did compile but still didnt exactly work but i thank both of you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.