YouTip LogoYouTip

C Examples Power Number

# C Programming Examples - Calculate the N-th Power of a Number [![Image 3: C Programming Examples](#) C Programming Examples](#) Calculate the N-th power of a number, for example: 2^3, where 2 is the base and 3 is the exponent. ## Example - Using while #includeint main(){int base, exponent; long long result = 1; printf("Base: "); scanf("%d", &base); printf("Exponent: "); scanf("%d", &exponent); while(exponent != 0){result *= base; --exponent; }printf("Result: %lld", result); return 0; } Output: Base: 2Exponent: 3Result: 8 ## Example - Using pow() function #include#includeint main(){double base, exponent, result; printf("Base: "); scanf("%lf", &base); printf("Exponent: "); scanf("%lf", &exponent); // Calculate result result = pow(base, exponent); printf("%.1lf^%.1lf = %.2lf", base, exponent, result); return 0; } Output: Base: 2Exponent: 32.0^3.0 = 8.00 ## Example - Recursion #includeint power(int n1, int n2); int main(){int base, powerRaised, result; printf("Base: "); scanf("%d",&base); printf("Exponent (positive integer): "); scanf("%d",&powerRaised); result = power(base, powerRaised); printf("%d^%d = %d", base, powerRaised, result); return 0; }int power(int base, int powerRaised){if(powerRaised != 0)return(base*power(base, powerRaised-1)); else return 1; } [![Image 4: C Programming Examples](#) C Programming Examples](#)
← C Examples Factors NumberC Examples Lcm β†’