Finding Exponential Without Pow Method

CCBeginner
Practice Now

Introduction

Exponential calculation is a very common requirement in many mathematical applications. In C programming, we can calculate exponentials using the pow() function. However, sometimes we might need to find the exponential of a number manually without using the pow() function. In this lab, we will go through the steps to find the exponential of a number without using the pow() function in C programming.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Declare variables

We start by declaring the variables we will use in our program. We will need an integer for the base number, an integer for the exponential value, and a long long int variable to store the result (since the result can be a very large number).

#include<stdio.h>

int main()
{
    int n, exp;
    long long int value = 1;

    // rest of the code goes here
}

Get user inputs

Next, we will get the input values for the base number and the exponential value from the user using the scanf() function.

#include<stdio.h>

int main()
{
    int n, exp;
    long long int value = 1;

    printf("Enter the base number and its exponential:\n\n");
    scanf("%d%d",&n, &exp);

    // rest of the code goes here
}

Calculate the exponential

We will now calculate the exponential by using a loop. The loop will execute the number of times equal to the exponential value entered by the user. We start with a value of 1, and for each iteration of the loop, we multiply the base number with the current value. We update the current value with the result of the multiplication. Finally, we print out the result.

#include <stdio.h>

int main()
{
    int n, exp;
    long long int value = 1;

    printf("Enter the base number and its exponential:\n\n");
    scanf("%d%d",&n, &exp);

    for(int i=1; i<=exp; i++)
    {
        value = value * n;
    }

    printf("\n\n %d^%d = %lld\n\n", n, exp, value);

    return 0;
}

Test the program

We can now test our program by running it and entering different base numbers and exponential values. If the program is working correctly, it should print out the correct results for each calculation.

Full code

The full code for finding the exponential of a number without using the pow() function is shown below.

#include <stdio.h>

int main()
{
    int n, exp;
    long long int value = 1;

    printf("Enter the base number and its exponential:\n\n");
    scanf("%d%d",&n, &exp);

    for(int i=1; i<=exp; i++)
    {
        value = value * n;
    }

    printf("\n\n %d^%d = %lld\n\n", n, exp, value);

    return 0;
}

Summary

In this lab, we learned how to find the exponential of a number without using the pow() function in C programming. We declared the necessary variables, got user inputs, and used a loop to calculate the exponential. We then printed out the result to the user.

Other C Tutorials you may like