C Program To Find Cube Of A Number Using Function

Share Your Love

In this post I am going to solve a problem to find cube of a number using function.

Here  input any number from user and find cube of the given number using function.

Example:

Input

Input any number: 5

Output

Cube of 5 = 125

Required Knowledge Before Going To Code:

  1. Basic C Programming.
  2. Function
  3. Returning value from function

Now Steps To Follow To Findout Cube Of a Number:

General formula cube of a number num is cube = num * num * num. This is easy way to write cube of number.

Step-1:

First give a meaningful name to function say cube().

Step-2:

Then the function should accept a number to evaluate a cube so the function definition is cube(double num)

Step-3:

Finally, the function should return cube of num passed. And return type of function should be double.

Now the program to find cube of a number:

/**
 * C program to find cube of any number using function
 */
#include <stdio.h>

/* Function declaration */
double cube(double num);

int main()
{
    int num;
    double c;
    
    /* Input number to find cube from user */
    printf("Enter any number: ");
    scanf("%d", &num);
    //function call and passing num
    c = cube(num);

    printf("Cube of %d is %.2f", num, c); 
    
    return 0;
}

/**
 * Function to find cube of any number
 */
double cube(double num)
{
    return (num * num * num);
}

Output:

Enter any number: 5
Cube of 5 is 125.00

Here, cube function store the temporary data of num passing through calling function, which is

double cube(double num)
{
    double c = num * num * num;
    return c;
}

%.2f prints fractional number up to 2 decimal places. You can also use %f, to print fractional numbers up to 6 decimal places (default).

If you like this post please comment and share this post and wants to contribute please WhatsApp us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter