Coding Practice

Write a C program to find power of a number using for loop

Find power of a number using for loop
Sample Output
Find power of a number using for loop.

Number: 2
Power: 4

2 Power 4 = 16
Source Code
#include<stdio.h>

int main()
{
    int i, num, power, result = 1;

    printf("Find power of a number using for loop.\n\nNumber: ");
    scanf("%d", &num);

    printf("Power: ");
    scanf("%d", &power);

    for(i = 0; i < power; i++)
    {
        result = result * num;
    }
    printf("\n%d Power %d = %d\n", num, i, result);
    
    return 0;
}
Sample Output
Find power of a number using for loop.

Number: 5
Power: 0

5 Power 0 = 1

No comments:

Post a Comment

Change Theme
X