Calculate power of a number without using pow() pre-defined function
This question is also asked in interviews frequently.
Please check below program:
- #include<stdio.h>
- int main()
- {
- int n, power;
- int i, j;
- int total = 0, sum;
- printf("Please enter Value and its power\n");
- scanf("%d%d", &n, &power);
- printf("Calculate: %d^%d:\n", n, power);
- total = n;
- for (i=1; i<power; i++) {
- sum = total;
- for (j=1; j<n; j++) {
- total += sum;
- }
- }
- printf("Result: %d\n", total);
- return 0;
- }
Please enter Value and its power
10
3
Calculate: 10^3:
Result: 1000
No comments:
Post a Comment