C program to find power of a number using for loop

Previous
Next

Input

            Input base: 2

            Input exponent: 5

Output

            2 ^ 5 = 32

#include <stdio.h>
int main()
{
	int base, exponent;
	long power = 1;
	int i;
	
	printf("Enter base : ");
	scanf("%d", &base);
	printf("Enter exponent : ");
	scanf("%d", &exponent);
	
	for(i=1; i<=exponent; i++)
	{
		power = power * base;
	}
	printf("%d ^ %d = %ld \n", base, exponent, power);
	return 0;
}
Previous
Next

Add Comment

Courses Enquiry Form