How to use Pre-defined mathematical function in C language?

CServer Side ProgrammingProgramming

Problem

How to find the cube root of any given number by using the C programming language?

Solution

Algorithm

Step 1: Enter any number at run time
Step 2: Read from console
Step 3: Compute result
        Result:pow(number,1.0/3.0)
Step 4: Increment result
Step 5: Print result

Example

Following is the C program to find the cube root of any given number −

//finding cube root of given number//
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
   int number, result;
   printf("Enter any number: ");
   scanf("%d",&number);
   result=pow(number, 1.0/3.0);
   result++;
   printf("\n\Cube of %d is: %d",number,result);
   getch();
}

Output

When the above program is executed, it produces the following result −

Cube of 4 is 64

Example

Consider another example by using math function.

#include <stdio.h>
#include <math.h>
int main (){
   float num1, num2, num3;
   printf("enter 3 numbers:\n");
   scanf("%f %f %f",&num1,&num2,&num3) ;
   printf ("number1 = %.1lf\n", ceil(num1));
   printf ("number2 = %.1lf\n", ceil(num2));
   printf ("number3 = %.1lf\n", ceil(num3));
   return(0);
}

Output

When the above program is executed, it produces the following result −

enter 3 numbers:
3.7 -4.2 -6.7
number1 = 4.0
number2 = -4.0
number3 = -6.0
raja
Published on 08-Mar-2021 10:29:13
Advertisements