Program To Find Cube In C || How to find Cube in c programming
Introduction
In this article, we present a comprehensive program written in the C programming language to find the cube of a given number. Our program utilizes efficient algorithms and follows best coding practices, ensuring both accuracy and performance. By following the step-by-step guide below, you will be able to implement the program seamlessly and obtain the cube of any number effortlessly.
Getting Started
Before we delve into the code, let’s first understand the concept of finding the cube of a number. The cube of a number is the result of multiplying the number by itself twice. For instance, the cube of 3 is calculated as 3 * 3 * 3, which equals 27.
How to find Cube in c programming
Example 1:
#include <stdio.h>
int findCube(int num) {
int cube = num * num * num;
return cube;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int cube = findCube(number);
printf("The cube of %d is %d.\n", number, cube);
return 0;
}
Program To Find Cube In C || How to find Cube in C programming
Example 2:
#include<stdio.h> void main () { int i,cube; scanf("%d",&i); for(int i=1;i<=5;i++){ cube=i*i*i; printf("\nnumber is %d and cube of the %d is :%d",i,i,cube); } }
How to find Cube in C programming >>>Explanation of the Program
Let’s walk through the code step by step to understand how the program works.
- We begin by including the necessary header file
stdio.h
, which provides input and output functionalities. - The
findCube
function takes an integernum
as input and calculates the cube by multiplying the number with itself twice. The result is stored in thecube
variable and then returned. - In the
main
function, we declare an integer variablenumber
to store the user input. - We prompt the user to enter a number using the
printf
function with the message “Enter a number: “. - The
scanf
function reads the user input and stores it in thenumber
variable. - We call the
findCube
function with thenumber
as the argument and store the returned value in thecube
variable. - Finally, we display the result using the
printf
function with the message “The cube of %d is %d.\n”, where%d
is a placeholder for the respective values ofnumber
andcube
.
Testing the Program
Now that we have explained the code, let’s test it with a few examples.
Example 1:
Input:
Enter a number: 5
Output:
The cube of 5 is 125.
Example 2:
Input:
Enter a number: -2
Output:
The cube of -2 is -8.
Example 3:
Input:
Enter a number: 0
Output:
The cube of 0 is 0.
Conclusion
Congratulations! You have successfully implemented the program to find the cube of a given number in C. Now you can calculate the cube of any number effortlessly using this program. We hope this article has provided you with a clear understanding of the code and its functionality.
Remember, understanding and practicing programming concepts is crucial for your growth as a programmer. Feel free to modify and enhance the program to suit your specific requirements. Happy coding!