C 语言中的对数计算程序
给定 n 的值作为输入,任务是通过一个函数计算 Log n 的值并显示它。
对数或 Log 是幂运算的反函数,这意味着要计算 log,必须将幂运算作为底数计算出来。
IF
$$\log_b x\;\:=\: y\:than\:b^{y}=x$$
例如
$$\log_2 64\;\:=\: 6\:than\:2^{6}=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
示例
#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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言程序设计
C++
C#
MongoDB
MySQL
JavaScript
PHP