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);
}
Re: having problem to debug this program. (C prog in linux) 80 80

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

Re: having problem to debug this program. (C prog in linux) 80 80

thanks

Be a part of the DaniWeb community

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