C Program to Find the Power of a Number

C Program to Find the Power of a Number

C program to find or calculate power of a number; Through this tutorial, we will learn how to find or calculate power of a number in the c program using for loop, while loop, and function.

Algorithm and Programs to Find the Power of a Number in C

  • Algorithm to Find the Power of a Number
  • C Program to Find the Power of a Number using Pow() Function
  • C Program to Find the Power of a Number using For Loop
  • C Program to Find the Power of a Number using While Loop

Algorithm to Find the Power of a Number

Use the following algorithm to write a program to find power of a number; as follows:

  • Step 1: Start Program
  • Step 2: Read the base and exponent number from user and store into it in variables
  • Step 3: Find the power of a number using the for loop or while loop or pow() function
  • Step 4: Print power of a number
  • Step 5: Stop Program

C Program to Find the Power of a Number using Pow() Function

#include <math.h>
#include <stdio.h>

int main() {
    double base, exp, result;
    printf("Enter a base number: ");
    scanf("%lf", &base);
    printf("Enter an exponent: ");
    scanf("%lf", &exp);

    // calculates the power
    result = pow(base, exp);

    printf("%.1lf^%.1lf = %.2lf", base, exp, result);
    return 0;
}

The output of the above c program; as follows:

Enter a base number: 10
Enter an exponent: 2
10.0^2.0 = 100.00

C Program to Find the Power of a Number using For Loop

/**
 * 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;
}

The output of the above c program; as follows:

Enter base: 10
Enter exponent: 2
10 ^ 2 = 100

C Program to Find the Power of a Number using While Loop

#include <stdio.h>
 
int main()
{
  int i = 1, Number, Exponent; 
  long Power = 1;
  
  printf("\n Enter base Number : ");
  scanf(" %d", &Number);

  printf("\n Enter an Exponent Number: ");
  scanf(" %d", &Exponent);
    
  while(i <= Exponent)
  {
  	Power = Power * Number;
  	i++;
  }
  
  printf("\n The Final result of %d Power %d = %ld", Number, Exponent, Power);
  
  return 0;
}

The output of the above c program; as follows:

Enter base Number : 10
Enter an Exponent Number: 2
The Final result of 10 Power 2 = 100

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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