C Program To Find The Power Of A Number Using for Loop

Share Your Love

In this post I am going to explain you how to find the power of a number using for loop?

Here a C program to find power of a number using for loop.

There is coming one question to find the power of a number without using built-in library functions in the C program. So, find the power of any number without using pow() function in C programming.

Example:

Input

Input base: 2
Input exponent: 5

Output

2 ^ 5 = 32

Logic to find the power of any number:

Step-1:

Input base and exponents from user and then store two variables say base and expo.

Step-2:

Then declare and initialize one variable say power=1.

Step-3:

Run the loop from 1 to expo and increment loop counter by 1 in each iteration. So the loop structure looks like for(i=1;i<=expo;i++)

Step-4:

For each iteration inside loop multiply power with num i.e. power = power*num

Step-5:

At the end of loop you print the power varibel.

Program to find power of any number:

/**
 * C program to find power of any number using for loop
 */

#include <stdio.h>

int main()
{
    int base, exponent;
    long long power = 1;
    int i;

    /* Input base and exponent from user */
    printf("Enter base: ");
    scanf("%d", &base);
    printf("Enter exponent: ");
    scanf("%d", &exponent);

    /* Multiply base, exponent times*/
    for(i=1; i<=exponent; i++)
    {
        power = power * base;
    }

    printf("%d ^ %d = %lld", base, exponent, power);

    return 0;
}

Keep Remember:

Some compilers didn’t understand long long data type so, you need to change the data type from long long to long type and also format string %lld to %ld.

Output:

Enter base: 2
Enter exponent: 5
2 ^ 5 = 32

Please comment and share the post and wants to contribute WhatsApp us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter