Knowee
Questions
Features
Study Tools

Write a C program to find cube of any number using function.

Question

Write a C program to find cube of any number using function.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that uses a function to calculate the cube of a number:

#include <stdio.h>

// Function declaration
double cube(double num);

int main() {
    double num, result;

    printf("Enter a number: ");
    scanf("%lf", &num);

    // Function call
    result = cube(num);

    printf("Cube of %.2lf = %.2lf", num, result);

    return 0;
}

// Function definition
double cube(double num) {
    return num * num * num;
}

In this program, we first declare a function cube() at the beginning. In the main() function, we ask the user for a number, and then call the cube() function with the number as an argument. The cube() function calculates the cube of the number by multiplying the number by itself twice and returns the result. The result is then printed out in the main() function.

This problem has been solved

Similar Questions

#include <stdio.h>int main(){ int num, cube; // Asking for Input printf("Enter an integer: "); scanf("%d", &num); cube = num * num * num; printf("Cube of %d is %d", num, cube); return 0;

write a c program Find the largest number among the three numbers.

Problem StatementImagine you are tasked with creating a program that calculates the cube of a given number. Your goal is to write a program that takes an integer n from the user, calculates its cube using a call-by-reference function, and then prints the result.Function Specifications: int cube(int *n) Input format :The input consists of a positive integer n.Output format :The output prints an integer which is the cube value of n.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 103Sample test cases :Input 1 :1Output 1 :1Input 2 :5Output 2 :125Input 3 :1000Output 3 :1000000000

Which function is used to find the cube root of a number?Marks : 1Negative Marks : 0Answer herecubert()cbrt()root3()cubicRoot()

Problem StatementKrish, a curious explorer in a unique computational environment, explores numerical transformations. The system swiftly calculates and displays the cube for positive numbers, while it diligently calculates and returns the square value for negative numbers.Write a program to achieve this and use the return statement accordingly.Input format :The input consists of a single integer n.Output format :The output displays "Cube value: " followed by the cube value of n if the number is positive and terminates.The output displays "Squared value: " followed by the squared value of n if the number is negative or zero and terminates.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:-100 ≤ n ≤ 100Sample test cases :Input 1 :100Output 1 :Cube value: 1000000Input 2 :-20Output 2 :Squared value: 400Input 3 :0Output 3 :Squared value: 0

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.