Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Friday, September 30, 2022

C program find cube of a number using function

C program find cube of a number using function


C program find cube of a number using function


Introduction

C programming language is a powerful tool for developing software applications. One of the basic mathematical operations that programmers perform is finding the cube of a number. In this article, we will demonstrate how C program find cube of a number using function.

What is a Cube?


A cube is a three-dimensional shape that has six square faces of equal size. It is also a number raised to the third power. In mathematics, the cube of a number is obtained by multiplying the number by itself three times. The symbol for the cube of a number is ^3.

How to find the Cube of a Number?


The cube of a number can be found by multiplying the number by itself three times. For example, the cube of 2 is calculated as 222, which equals 8. Similarly, the cube of 5 is calculated as 555, which equals 125.

Functions in C Programming Language


A function is a reusable block of code that is designed to perform a specific task. It is a collection of statements that are enclosed within braces and are executed when the function is called. In C programming language, functions are an essential part of the language.

Functions in C programming language have the following advantages.
  • They help in code reusability.
  • They improve the readability of the code.
  • They make the program modular.
  • They help in debugging the code.

Writing a Function to Calculate the Cube of a Number


To calculate the cube of a number using a function in C programming language, we need to write a function that takes an integer as input and returns the cube of that integer as output.

The following code demonstrates how to write a function to calculate the cube of a number:

#include<stdio.h>
float cu(long n)
{
long x;
x=n*n*n;
return x;
}
int main()
{
long x,y;
printf("Enter any Number : ");
scanf("%ld",&x);
y=cu(x);
printf("\nCube of %ld = %ld",x,y);
return 0;
}


Explanation of the program:


This is a C program that finds the cube of a number using a function. The program starts by defining a function called cu that takes a long integer as input and returns a float value. The cu function calculates the cube of the input number by multiplying it with itself three times and stores the result in a variable x. Then it returns the value of x.

In the main function, the program prompts the user to enter a number using printf and reads the input number using scanf. It then calls the cu function with the input number as an argument and stores the result in the variable y. Finally, it prints the result using printf.

Let's walk through the code step by step:

#include<stdio.h> 
float cu(long n) 
long x; 
x = n * n * n; 
return x; 
}
This part of the code defines the cu function that takes a long integer as input and returns a float value. Inside the function, it calculates the cube of the input number by multiplying it with itself three times and stores the result in a variable x. Then it returns the value of x.

int main() 
long x, y; 
printf("Enter any Number : "); 
scanf("%ld", &x); 
y = cu(x); 
printf("\nCube of %ld = %ld", x, y); 
return 0; 
}

This part of the code defines the main function. Inside the main function, it declares two long integer variables, x and y. It prompts the user to enter a number using printf and reads the input using scanf. It then calls the cu function with the input number as an argument and stores the result in the variable y. Finally, it prints the result using printf.

Output:



Conclusion

In this article, we discussed how C program find cube of a number using function. We first explained what a cube is and how to find the cube of a number mathematically. We then discussed the C programming language, functions in C, and how to write a function to calculate the cube of a number. Finally, we demonstrated how to write a complete C program to find the cube of a number using a function.

FAQs

1) What is a function in C programming language?
Ans: A function is a reusable block of code that is designed to perform a specific task. It is a collection of statements that are executed when the function is called.

2) What is the cube of a number?
Ans: The cube of a number is obtained by multiplying the number by itself three times.

3) What is the advantage of using functions in C programming language?
Ans: Functions make the code modular, reusable, and easier to read and debug.

4) How can we find the cube of a number in C programming language?
Ans: We can find the cube of a number by multiplying the number by itself three times.

5) Can we use a function in multiple parts of a C program?
Ans: Yes, functions can be reused in multiple parts of a program, reducing the amount of code that needs to be written.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages