### C# Basic

### C# Interview Programs

- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number

# C# Program to Check for a Cube Number

Photo Credit to CodeToFun

## π Introduction

In the realm of programming, mathematical properties of numbers often lead to interesting and practical applications. One such property is being a cube number. A cube number is an integer that is the cube of an integer.

In this tutorial, we'll delve into a C# program that efficiently checks whether a given number is a perfect cube or not.

## π Example

Let's take a look at the C# code that accomplishes this functionality.

```
using System;
class Program {
// Function to check if a number is a perfect cube
static bool IsPerfectCube(int number) {
// Calculate the cube root of the number
double cubeRoot = Math.Cbrt(number);
// Check if the cube root is close to an integer
return Math.Abs(cubeRoot - Math.Round(cubeRoot)) < 1e-10;
}
// Driver program
static void Main() {
// Replace this value with your desired number
int number = 27;
// Check if the number is a perfect cube
if (IsPerfectCube(number)) {
Console.WriteLine($"{number} is a perfect cube.");
} else {
Console.WriteLine($"{number} is not a perfect cube.");
}
}
}
```

### π» Testing the Program

To test the program with different numbers, simply replace the value of the number variable in the Main method.

27 is a cube number.

Compile and run the program to check if the number is a perfect cube.

### π§ How the Program Works

- The program defines a function IsPerfectCube that takes an integer as input and returns true if it is a perfect cube, and false otherwise.
- Inside the function, it calculates the cube root of the number using the Math.Cbrt method.
- It then checks if the cube root is close to an integer using a tolerance value (1e-10 in this case).
- The Main method initializes a variable with a number and calls the IsPerfectCube function to determine if it's a perfect cube.

## π Between the Given Range

Let's delve into the C# code that identifies cube numbers in the specified range.

```
using System;
class Program {
// Function to check if a number is a cube number
static bool IsCubeNumber(int num) {
int cubeRoot = (int) Math.Round(Math.Pow(num, 1.0 / 3.0));
int cube = cubeRoot * cubeRoot * cubeRoot;
return cube == num;
}
// Driver program
static void Main() {
Console.WriteLine("Cube numbers in the range 1 to 50:");
// Check and display cube numbers in the range
for (int i = 1; i <= 50; i++) {
if (IsCubeNumber(i)) {
Console.Write($"{i} ");
}
}
Console.WriteLine();
}
}
```

### π» Testing the Program

Cube numbers in the range 1 to 50: 1 8 27

Run the program to see the cube numbers within the range of 1 to 50.

### π§ How the Program Works

- The program defines a function IsCubeNumber that checks if a given number is a cube number.
- Inside the function, it calculates the cube root of the number and checks if the cube of this root is equal to the original number.
- The Main method iterates through numbers from 1 to 50 and prints those that are cube numbers.

## π§ Understanding the Concept of Cube Number

A cube number is the result of raising an integer to the power of 3. For example, 2 * 2 * 2 = 8, so 8 is a cube number.

## π’ Optimizing the Program

The provided program is straightforward, but you might consider optimizing it further. For instance, you can explore alternative methods for checking if a number is a cube number without using the cbrt function.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

## π¨βπ» Join our Community:

## Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee
If you have any doubts regarding this article (

C# Program to Check for a Cube Number), please comment here. I will help you immediately.