Write a C program to find power of any number x ^ y.

  • The program first declares three variables: base, exponent, and result initialized to 1.
  • It then prompts the user to enter the base and exponent using the printf() and scanf() functions.
  • A for loop is used to calculate the power of the number. The loop starts with i equal to 1 and continues until i is less than or equal to exponent. In each iteration of the loop, base is multiplied by result.
  • Finally, the program prints the result using the printf() function.
  • Note that the result variable is initialized to 1 before the loop starts because any number raised to the power of 0 is 1.
#include <stdio.h>

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

    printf("Enter base: ");
    scanf("%d", &base);

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

    for (int i = 1; i <= exponent; i++) {
        result *= base;
    }

    printf("%d^%d = %d", base, exponent, result);

    return 0;
}

OUTPUT

Enter base: 2
Enter exponent: 5
2^5 = 32

For Trending Topics

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *