
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to calculate power of a given number
Take two integers from the user for base and exponent and calculate the power as explained below.
Example
Consider the following for writing a C program.
- Suppose base =3
- Exponent = 4
- Power=3*3*3*3
Algorithm
Follow the algorithm given below −
Step 1: Declare int and long variables. Step 2: Enter base value through console. Step 3: Enter exponent value through console. Step 4: While loop. Exponent !=0 i. Value *=base ii. –exponent Step 5: Print the result.
Example
The following program explains how to calculate power of given number in C language.
#include<stdio.h> int main(){ int base, exponent; long value = 1; printf("Enter a base value:
"); scanf("%d", &base); printf("Enter an exponent value: "); scanf("%d", &exponent); while (exponent != 0){ value *= base; --exponent; } printf("result = %ld", value); return 0; }
Output
When the above program is executed, it produces the following result −
Run 1: Enter a base value: 5 Enter an exponent value: 4 result = 625 Run 2: Enter a base value: 8 Enter an exponent value: 3 result = 512
Example
If we want to find the power of real numbers, we can use pow function which is a predefined function present in math.h.
#include<math.h> #include<stdio.h> int main() { double base, exponent, value; printf("Enter a base value: "); scanf("%lf", &base); printf("Enter an exponent value: "); scanf("%lf", &exponent); // calculates the power value = pow(base, exponent); printf("%.1lf^%.1lf = %.2lf", base, exponent, value); return 0; }
Output
When the above program is executed, it produces the following result −
Enter a base value: 3.4 Enter an exponent value: 2.3 3.4^2.3 = 16.69
- Related Articles
- C++ Program to Calculate Power of a Number
- Java program to calculate the power of a Given number using recursion
- Java program to calculate the power of a number
- Swift Program to Calculate the Power of a Number
- Haskell Program to Calculate the Power of a Number
- Kotlin Program to Calculate the Power of a Number
- Calculate the Power of a Number in Java Program
- C++ Program to calculate the logarithm of a given number based on a given base
- How to calculate Power of a number using recursion in C#?
- C++ Program to Calculate Power Using Recursion
- C++ Program to calculate the cube root of the given number
- C++ Program to calculate the logarithm gamma of the given number
- C++ Program to calculate the number of odd days in given number of years
- Golang Program to check the power of 4 of a given number
- JavaScript program to check whether a given number is power of 2

Advertisements