Cube Root Program In C


Advertisements

Finding that a given number is even or odd, is a classic C program. We shall learn the use of conditional statement if-else in C.

Algorithm

Algorithm of this program is very easy −

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

Flow Diagram

We can draw a flow diagram for this program as given below −

FlowDiagram Even Odd

Pseudocode

procedure even_odd()
   
   IF (number modulo 2) equals to 0
      PRINT number is even
   ELSE
      PRINT number is odd
   END IF

end procedure

Implementation

Implementation of this algorithm is given below −

#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;
}

Output

Output of the program should be −

Cube root of 125 = 5.000000
mathematical_programs_in_c.htm
Advertisements