Monday, April 28, 2025

C Program - Calculate Power of a Number using While Loop

This C program calculates the power of a base number raised to an exponent entered by the user.

It prompts the user to enter the base number and exponent using printf() and scanf(), respectively. It then uses a while loop to calculate the power of the base number raised to the exponent. Inside the loop, it multiplies the result variable by base_number for each iteration until the temp_exponent reaches 0.

After the loop, it prints the final value of result using printf().

#include <stdio.h>

int main()
{

    int base_number, exponent;
    int result = 1;

    printf("Enter the Base Number: \n");
    scanf("%d", &base_number);

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

    int temp_exponent = exponent;

    while (temp_exponent != 0)
    {
        result = result * base_number;
        printf("Result atm: %d \n", result);
        printf("Exponent atm: %d \n", temp_exponent);
        --temp_exponent;
    }

    printf("Final Result: %d \n", result);

    return 0;
}

Result: 

Enter the Base Number:
10
Enter Exponent:
3
Result atm: 10
Exponent atm: 3
Result atm: 100
Exponent atm: 2
Result atm: 1000
Exponent atm: 1
Final Result: 1000

Process returned 0 (0x0)   execution time : 4.000 s
Press any key to continue.

Here's an explanation of the code line by line: 

#include <stdio.h>

This line includes the standard input-output library in C programming. 

int main()
{
    int base_number, exponent;
    int result = 1;

This is the main function of the program. It declares three integer variables: base_number, exponent, and result. result is initialized to 1. 

    printf("Enter the Base Number: \n");
    scanf("%d", &base_number);

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

These printf() statements display prompts for the user to enter the base_number and exponent values respectively. The scanf() function reads in integer values for these variables from the user input using the %d format specifier. 

    int temp_exponent = exponent;

    while (temp_exponent != 0)
    {
        result = result * base_number;
        printf("Result atm: %d \n", result);
        printf("Exponent atm: %d \n", temp_exponent);
        --temp_exponent;
    }

This while loop executes result = result * base_number for each value of temp_exponent from exponent down to 0. The --temp_exponent expression decrements temp_exponent by 1 at the end of each iteration of the loop. Inside the loop, printf() statements display the current value of result and temp_exponent

    printf("Final Result: %d \n", result);

    return 0;
}

After the loop is completed, the final value of result is displayed using printf(). The program ends with a return value of 0.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...