0

A program that accepts two positive integer values x and n, and uses a function to calculate the value of x raised to the power n. The function should calculate power by repeated multiplications. The result should be returned to the main function and displayed.

I use linux terminal to run this program.There is no error but its returning me "Answer =0 "

Whats wrong with these lines of codes?

int power(a,b)
{
	do
	{
		a=a*a;
		b=b-1;
	}
	while (b>=1);
	return 0;
}


int main()
{
	int x,n;
	int ans;

	printf("Enter the value of x: ");
	scanf("%d",&x);
	printf("Enter the value of n: ");
	scanf("%d", &n);
	ans = power(x,n);
	
	printf("Answer = %d \n", ans);
}
2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by techyworld
0

It's printing "Answer = 0" because you're printing the return value of power(), and you have power() returning 0 every time.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.