zhe41223118/cp2023

  • Home
    • SMap
    • reveal
    • blog
  • About
  • w15
    • try Do
    • ANS 1
    • ANS 2
  • w13
    • step 1
    • step 2
    • step 3
    • bad
  • w12
  • w11
  • w8
  • w7
    • Hello
    • Hello 迴圈與重複
    • Hellogd2
    • korea
  • w4-w5
    • INFO
    • mass-spring-damper
    • Euler Method
  • replit
    • part 1
    • part 2
    • part 3
  • set wifi
  • work
  • program
  • file
  • w17
  • Brython
part 1 << Previous Next >> part 3

part 2

X次方數值計算

#include <stdio.h>
#include <limits.h>

// 定義計算 x 的 n 次方的函式
double powxn(double x, int n) {
    double k;
    if (n == 0) return 1;  // 若 n 為 0,則 x 的 0 次方為 1。
    k = powxn(x * x, n / 2);  // 傳回計算 x^2 的 (n/2) 次方。
    if (n % 2) k = k * x;  // 若 n 為奇數,則乘上額外的 x。
    return k;
}

int main(void)
{
    double x = 2;
    int n = 10;
    printf("\nx = %f, y = %d  ", x, n);
    printf("\nResult:(x^n) : %f ", powxn(x, n));

    x = 3;
    n = 20;
    printf("\n\nx = %f, y = %d  ", x, n);
    printf("\nResult:(x^n) : %f ", powxn(x, n));

    return 0;
}


part 1 << Previous Next >> part 3

Copyright © All rights reserved | This template is made with by Colorlib