
C 立方根程序
判断一个给定的数字是偶数还是奇数是一个经典的 C 语言程序。我们将在 C 语言中学习使用条件语句 if-else
。
算法
此程序的算法非常简单 −
START Step 1 → Take integer variable A Step 2 → Assign value to the variable Step 3 → Perform A modulo 2 and check result if output is 0 Step 4 → If true print A is even Step 5 → If false print A is odd STOP
流程图
可以为该程序绘制一个流程图,如下所示 −

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
伪代码
procedure even_odd() IF (number modulo 2) equals to 0 PRINT number is even ELSE PRINT number is odd END IF end procedure
实现
此算法的实现如下 −
#include <stdio.h> double cubeRoot(double n) { double i, precision = 0.000001; for(i = 1; (i*i*i) <= n; ++i); //Integer part for(--i; (i*i*i) < n; i += precision); //Fractional part return i; } int main() { int n = 125; printf("Cube root of %d = %lf", n, cubeRoot(n)); return 0; }
输出
程序输出应为 −
Cube root of 125 = 5.000000
mathematical_programs_in_c.htm
广告