Raise Number To Power C Program

By | September 19, 2015

C Program To Raise Number To Power

Learn How To Raise Number To Power in C Programming Language. It is important that we should know How A For Loop Works before getting further with this C Program Code Tutorial.

Example

Base Number = 3

Power = 2

Output: 32 = 9

Method 1: Raise A Number To Power in C Programming with While Loop

#include<stdio.h>

int main()
{
    int base_num, pow_num, value = 1;
    printf("\nEnter the Number:\t");  
    scanf("%d", &base_num);
    printf("\nEnter the Power:\t");
    scanf("%d", &pow_num);
    while(pow_num != 0)
    {
        value = value * base_num;
        pow_num--;
    }
    printf("\nResult = %d\n", value);
    return 0;
}

Also Read: C Program To Raise A Integer To Power using Recursion Method

Method 2: C Program To Raise Number To Positive Power using For Loop

#include<stdio.h>

int main()
{
    int base_num, pow_num, value = 1;
    printf("\nEnter the Number:\t");  
    scanf("%d", &base_num);
    printf("\nEnter the Power:\t");
    scanf("%d", &pow_num);
    for(value = 1; pow_num != 0; pow_num--)
    {
        value = value * base_num;
    }
    printf("\nResult = %d\n", value);
    return 0;
}

Method 3: C Program To Raise an Integer To Power using Function

#include<stdio.h>

int raise_number(int power, int base)
{
    int value = 1;
    while(power != 0)
    {
        value = value * base;
        power--;
    }
	return value;
}

int main()
{
    int pow_num, base_num;
    printf("\nEnter the Number:\t");  
    scanf("%d", &base_num);
    printf("\nEnter the Power:\t");
    scanf("%d", &pow_num);
    printf("\nResult = %d\n", raise_number(pow_num, base_num));
    return 0;
}

Output

Raise Number To Power in C Programming

In case you get any Compilation Errors with this C Program To Raise Number a Power or if you have any doubt about it, let us know about it in the Comment Section below.

One thought on “Raise Number To Power C Program

  1. Pankaj Dhende

    I liked the conversion of For Loop into While Loop. Can we raise number to integer in C using Recursion?

    Reply

Let's Discuss