Your given program uses 4-bytes integer which is capable to hold the values between -2,147,483,648 to 2,147,483,647 but 10^10 is too large to hold it.
Taking the appropriate type (such as long long
) to hold such large outcome will easily solve your issue (notice the comments as an explanation to the code):
#include <stdio.h>
// The function signature
void power(int *x, int *y);
int main(void) {
int bs, pw;
printf("Enter the value of base and power: ");
// Verifying the inputs
if (scanf("%d%d", &bs, &pw) != 2)
// If the values were incorrectly entered, then it returns
// with an error code without further actions
return 1;
power(&bs, &pw);
return 0;
}
// The function definition
void power(int *x, int *y) {
long long result = 1;
// Dereferencing and using assignment operator for self-multiplication with
// the dereferenced 'x'
for (int i = 0; i < *y; i++)
result *= *x;
printf("Results: %lld\n", result);
}
A sample test case:
Enter the value of base and power: 10 10
Results: 10000000000
Works on OnlineGDB as well.