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);
}
Member Avatar for Mouche

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

thanks

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.