Program To Find Cube In C || Free Code Center

Program To Find Cube In C

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.

Program To Find Cube In C

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

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);
    }
}

Explanation of the Program

Let’s walk through the code step by step to understand how the program works.

  1. We begin by including the necessary header file stdio.h, which provides input and output functionalities.
  2. The findCube function takes an integer num as input and calculates the cube by multiplying the number with itself twice. The result is stored in the cube variable and then returned.
  3. In the main function, we declare an integer variable number to store the user input.
  4. We prompt the user to enter a number using the printf function with the message “Enter a number: “.
  5. The scanf function reads the user input and stores it in the number variable.
  6. We call the findCube function with the number as the argument and store the returned value in the cube variable.
  7. 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 of number and cube.

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!

C programming tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
NEW TOXIC ROCKET CYCLE DECK in Clash Royale 2023 Best Deck in Clash Royale Best Deck for Arena 8 in Clash Royale Best Hog deck for Pro players Best Clash Royale Deck