If you’ve noticed, In C Programming, you cannot use **a^b** directly to find the value of an exponent. Although, there is a **pow(a, b) **function that gives you the result. But, it’s always nice to work with the basic logic.

If you look at the definition of an exponential:

“Exponentiation is a mathematical operation, written as a^n. When n is a positive integer, exponentiation corresponds to repeated multiplication.” – Wikipedia

We’re gonna work out our logic using this very basic definition [*especially the part where it says* – exponentiation corresponds to repeated multiplication ;)]

Code:

main()

{

int base, index;

printf(“Enter the base\n“); /* say, 2 */

scanf(“%d“, &base);

printf(“Enter the index\n“); /* say, 3 */

scanf(“%d“, &index);/* always remember that 2^3 is actually 2x2x2 ie-2 multiplied by itself 3 times */

int i;

int power=base; /* power is initiallized to 2 */

for(i=1;i<index;i++)

{

power=power*base; /* 1st loop : power = 2*2, 2nd loop : power =(2*2)*2 */

}printf(“\n%d“, power);

getch();

}

It’s pretty easy to understand using an example. Refer to the example in comments to see how the code works. Also, this is not the only way. Think of how you could achieve the same with a different (and perhaps, better) code.