Chapter - 6: Functions And Pointers

Write a function power ( a, b ), to calculate the value of a raised to b


D
Sections
2
Exercises
#include<stdio.h>
#include<conio.h>

int power(int, int);

int main()
{
	int a, b, ans;
	printf("Enter the base and the power : ");
	scanf("%d%d", &a, &b);
	ans = power(a, b);
	printf("%d raised to the power %d = %d\n", a, b, ans);
	_getch();
	return 0;
}

int power(int x, int y)
{
	int num = 1, i;
	for (i = 1; i <= y; i++)
		num = num*x;
	return(num);
}

© 2020 Garbage Valuegarbage value logo