C program to find cube of a number using function

Write a C program to input any number from user and find cube of the given number using function. How to find cube of a given number using function in C programming. C function to find cube of any number.

Example:
Input any number: 5
Output: 125

Required knowledge

Basic C programming, Functions

Till now we already know how to find power of any number (a^b) using pow() function and also finding power of any numbers using loop. Here we will be writing our custom function to find cube of any number using functions.

Program to find cube using function

/**
 * C program to find cube of any number using function
 */
 

#include <stdio.h>


/**
 * Function to find cube of any number
 * @num Number whose cube is to be calculated
 */
double cube(double num)
{
    return (num * num * num);
}



int main()
{
    int num;
    double c;
    
    printf("Enter any number: ");
    scanf("%d", &num);
    
    c = cube(num);

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


Output
Enter any number: 5
Cube of 5 is 125.00
6, 28, 496, 8128,

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment