Power of the number

Find power of the Number

C program to find power of the number

Here we write a logic, which calculates the power of any number, by taking the values of base and exponent from the user and calculates the power.

For example: In the case of  32
3 is the base number
2 is the exponent
And, the power is equal to 3*3=9

#include <stdio.h>
#include<conio.h>
void main()
{
    int base,expo, exponent;
    float power = 1;
    int i;
       clrscr();
    /* Take base and exponent as input*/
printf("\n\t\t\t...Welcome To EduNews.Tech... ");
    printf("\n\nEnter base: ");
    scanf("%d", &base);
    printf("Enter exponent: ");
    scanf("%d", &exponent);
    expo = exponent;
    while (expo < 0)
    {
        {
            power = power/base;
            expo++;
        }
    }
    if(exponent >0)
    {
 
        /*Calculate power */
        for(i = 1; i <= exponent; i++)
        {
            power = power * base;
        }
    }
    printf("%d ^ %d = %f", base, exponent, power);
    printf("\n\n\n\t\t\tThankyou for Joining Us !");
    printf("\n\t\t\t!Regards EduNews !");
    
getch();
}

Program Output:

power of the number

Program Explanation

  1. We have to take two inputs from the user, base and the exponent to calculate the power of a number using a loop. We have to initialize a variable named power as 1.
  2. The power variable stores the final value of the power.
  3. Run a loop starting from index value 1 till the value of the exponent, and multiply power with base and store the result in power in each iteration. After it comes out of the loop, power has the output value in it, and we print that value.

I hope this post helps you to understand the “Power of the number” and its implementation in C programming language.

Keep coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *