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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/comments -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/variables -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/data_types -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/operators -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/for_loop -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/user_input -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/memory_address -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/pointers -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/function_parameters -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} c/function_declaration -.-> lab-197936{{"`Finding Exponential Without Pow Method`"}} end

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