C 语言中的对数计算程序
给定 n 的值作为输入,任务是通过一个函数计算 Log n 的值并显示它。
对数或 Log 是幂运算的反函数,这意味着要计算 log,必须将幂运算作为底数计算出来。
IF
logbx=ythanby=x
例如
log264=6than26=64
示例
Input-: Log 20 Output-: 4 Input-: Log 64 Output-: 6
算法
Start In function unsigned int log2n(unsigned int num) Step 1-> Return (num > 1) ? 1 + log2n(num / 2) : 0 In function int main() Step 1-> Declare and assign num = 20 Print log2n(num) Stop
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
#include <stdio.h> //We will be using recursive Approach used below is as follows unsigned int log2n(unsigned int num) { return (num > 1) ? 1 + log2n(num / 2) : 0; } int main() { unsigned int num = 20; printf("%u
", log2n(num)); return 0; }
输出
4
广告