Write a function power( a, b ), to calculate the value of a raised to b.
Note: In today’s video tutorial lets see 2 methods of calculating value of a raised to b.
1. In first method lets write the entire logic ourselves.
2. In second method lets use the built in method pow() which is present in math.h library file.
Problem Statement
To clarify, the problem statement is: User inputs two numbers through the keyboard. Write a C program to find the value of one number raised to the power of another, using function / method.
Related Read:
Function / Methods In C Programming Language
Calculate Power of a Number: C Program
Calculate Power of a Number using pow(): C Program
Video Tutorial: C Program To Calculate One Number Raised To Another using Function
[youtube https://www.youtube.com/watch?v=NN_m-I_2auE]
Source Code: C Program To Calculate One Number Raised To Another using Function
#include<stdio.h> int power(int base, int exp) { int count, result = 1; for(count = 1; count <= exp; count++) { result = result * base; } return(result); } int main() { int a, b; printf("Enter integer values for base and exponent\n"); scanf("%d%d", &a, &b); printf("%d to the power of %d is %d\n", a, b, power(a, b)); return 0; }
Output 1:
Enter integer values for base and exponent
2
4
2 to the power of 4 is 16
Output 2:
Enter integer values for base and exponent
3
2
3 to the power of 2 is 9
Output 3:
Enter integer values for base and exponent
5
2
5 to the power of 2 is 25
Output 4:
Enter integer values for base and exponent
5
3
5 to the power of 3 is 125
Logic
In above code, we ask the user to enter base and exponent values. We pass these 2 values to function power(). Inside power() function/method, we use for loop to multiply the value of base exponent number of times. This would give us the result and we return the result.
Example: In above code, if the user inputs 2 as base and 4 as exponent, then: for loop executes or iterates exponent number of times i.e., 4 times in our case. So 2 is multiplied 4 times.
2 x 2 x 2 x 2 = 16.
Source Code: C Program To Calculate One Number Raised To Another using Function and builtin method pow()
#include<stdio.h> #include<math.h> float power(int base, int exp) { return( pow(base, exp) ); } int main() { int a, b; printf("Enter integer values for base and exponent\n"); scanf("%d%d", &a, &b); printf("%d to the power of %d is %0.2f\n", a, b, power(a, b)); return 0; }
Output 1:
Enter integer values for base and exponent
2
4
2 to the power of 4 is 16
Output 2:
Enter integer values for base and exponent
3
2
3 to the power of 2 is 9
Output 3:
Enter integer values for base and exponent
5
2
5 to the power of 2 is 25
Output 4:
Enter integer values for base and exponent
5
3
5 to the power of 3 is 125
Logic
Here we ask the user to enter the value of base and exponent. We pass the values of base and exponent to power() method. Inside power() method we use pow() builtin method which is present in math.h header file. We pass base and exponent value to pow() function and it returns the result, which we return to calling function i.e., main method.
Note:
pow() method is present in math.h library, so we include it at the top of our c source code.
Important Note: Note that we are using float as return type in above program(second method), since built in method pow() returns floating point or double type data. If we use integer type data as return type then it would give wrong results for certain inputs.
For example, if you have below code for power() function
int power(int base, int exp) { return( pow(base, exp) ); }
If would return 24 for 5 raise to 2. And 124 as result for 5 raise to 3. Both these results are wrong.
Important Note: As you might have observed already we are not using function prototype in above programs. That is because we are writing function definition at the top before main function from where we are calling power() function. So main method already knows that there is a method called power() in our program.
If we want to write more than 1 function and we need to arrange it properly, we can write function prototype at the top – so that we can know about the functions present in our program in one glance, in one place. And then we can write the function definition anywhere in our program, in any order. Function prototype and function definition orders do not matter.
Also note that, writing function prototype is optional if you write function definition before main.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert