C Program To Calculate One Number Raised To Another using Function

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]

YouTube Link: https://www.youtube.com/watch?v=NN_m-I_2auE [Watch the Video In Full Screen.]


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

Calculate Power of a Number using pow(): C Program

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another by using pow() method present in math.h library.

Related Read:
while loop in C programming
Calculate Power of a Number: C Program

One Number Raised To Another using pow(): C Program


[youtube https://www.youtube.com/watch?v=i-aop38U0Hg]

YouTube Link: https://www.youtube.com/watch?v=i-aop38U0Hg [Watch the Video In Full Screen.]


Source Code:One Number Raised To Another using pow(): C Program

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

int main()
{
    int base, exponent;

    printf("Enter base value\n");
    scanf("%d", &base);

    printf("Enter exponent value\n");
    scanf("%d", &exponent);

    printf("%d to the power of %d is %2f\n",
           base, exponent, pow(base, exponent));

    return 0;
}

Output:
Enter base value
25
Enter exponent value
3
25 to the power of 3 is 15625.000000

Note:
pow() method is present in math.h library, so we include it at the top of our c source code.

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

Calculate Power of a Number: C Program

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.

Related Read:
while loop in C programming

In this program, we ask the user to input values for base and exponent. We use while loop in this program. The while loop is executed “exponent” times. And inside while loop we multiply base value and output the result.

One Number Raised To Another: C Program


[youtube https://www.youtube.com/watch?v=bVhc7kuaJtM]

YouTube Link: https://www.youtube.com/watch?v=bVhc7kuaJtM [Watch the Video In Full Screen.]


Source Code:One Number Raised To Another: C Program

#include < stdio.h >

int main()
{
    int base, exponent, res = 1, temp;

    printf("Enter the base\n");
    scanf("%d", &base);

    printf("Enter the exponent\n");
    scanf("%d", &exponent);

    temp = exponent;

    while(exponent)
    {
        res = res * base;
        exponent--;
    }

    printf("%d to the power of %d is %d\n", base, temp, res);

    return 0;
}

Output:
Enter the base
5
Enter the exponent
4
5 to the power of 4 is 625

Note:
1. We are initializing value of variable res to 1 in order to make sure it doesn’t have garbage value in it. If it was an addition operation we could have assigned it a initial value of 0. But in above program we are doing multiplication. So multiplying any number with 0 will give 0 as result. So we initialize the value of variable res to 1.

2. We modify the value of variable exponent in while loop. So to preserve the user entered value for exponent, we take another variable temp and store the original number(exponent value) entered by the user.

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