10+ C# Cube Root Finder Program Examples

This post contains a total of 10+ Hand-Picked C Sharp Cube Root Finder Program Examples with Source Code. All these Cube root programs are made using C Sharp Programming language.

You can use the source code of these examples with credits to the original owner.

Related Posts

C Sharp Cube Root Finder Programs

1. By Vedang

Made by Vedang. C Sharp program to get the cube root of a number. Source

Enter the Number : 5

Cube Root of 5 is 1.70998

using @System;
using @XPL;

namespace @XPL
{
    class @Text
    {
        public static void @Set(string x)
        {
            Console.WriteLine(x);
        }
        public static string @Get()
        {
            return Console.ReadLine();
        }
        public static int @ToInt(object x)
        {
            return Convert.ToInt32(x);
        }
        public static double @ToDouble(object x)
        {
            return Convert.ToDouble(x);
        }
    }
}
namespace @Made_BY_VEDANG
{
    class @Code
    {
        public static void Main()
        {
            double num, res,p = (double)1/3;
            Text.Set("Enter the Number : ");
            var n = Text.Get();
            try
            {
                num = Text.ToDouble(n);
                res = Math.Round((Math.Pow(num, p)), 5);
                Text.Set("\nCube Root of " + num + " is " + res);
            }
            catch (FormatException)
            {
                Text.Set("Invalid Input.");
            }
            Text.Get();
        }
    }
}

2. By Vladlena Dolomanova

Made by Vladlena Dolomanova. C Sharp Program to find Cube Root without using any standard functions. Source

Input: 10
Output: 2.1544
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class CubeCalculator
    {
        private double input;
        private double cube;
        private double number;
        private const double step = 0.0001; 
        
        public CubeCalculator(string a)
        {
            input = Double.Parse(a);    
        }

        public void Calculate()
        {
            double i = 0;
            do
            {
                cube = i * i * i;
                number = i;
                i += step;
            }
            while (cube < input);
            number -= step;                  
        }
        
        public void Info()
        {
            Console.WriteLine("Input: {0}", input);
            Console.WriteLine("Output: {0:0.####}", number);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            CubeCalculator user = new CubeCalculator(Console.ReadLine());
            user.Calculate();
            user.Info();
        }
    }
}

3. By Javier Felipe Toribio

Made by Javier Felipe Toribio. Program to find the cube root of a number N to a precision of 0.0001. Source

12
Cube root of 12 is 2.289424896240234375
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        /*
         * Class for generic root.
         * Constructor parameters set:
         *  + Integer exponent of the root.
         *  + Number with generic numeric type
         * 
         */
        class Root<T>
        {
            private const decimal prec=
                0.0001M;
            private readonly decimal
             min_candidate;
            private int exponent;
            private decimal target;
            private decimal? result=null;
            
            /*
             * Constructor
             */
            public Root(int exponent,T target)
            {
                this.exponent=exponent;
                
                this.target=
                  Convert.ToDecimal(target);
                  
                min_candidate=power(prec);
                
                if (this.target >=
                    Decimal.Zero) {
                    
                    calculate
                        (Decimal.Zero,
                         this.target,
                         this.target/2.0M);
                    
                } else {
                    
                    calculate
                        (this.target,
                         Decimal.Zero,
                         this.target/2.0M);
                }
            }
            
            /*
             * getter of calculation result
             */
            public decimal? Result
             { get; private set; }
             
            /*
             * return image of result
             */
            public override String ToString()
             {  
                 return
                   (Result.HasValue ?
                    Result.Value.ToString() :
                   "undefined");
             }
            
            /*
             * Raise a number to the power of
             * exponent
             */
            private decimal power(decimal num, 
                                  int count=1)
            {
                if (count==exponent)
                  return num;
                return num*power(num,count+1);
            }
            
            /*
             * Check if power is close to
             * target
             */
            private bool is_target
                (decimal num)
            {
                return
                 (Math.Abs(target-num)<=prec);
            }
            
            /* 
             * Search root recursively in
             * decreasing ranges
             */
            private void calculate
                (decimal from,
                 decimal to,
                 decimal candidate)
            {
               
               decimal p = power(candidate);
               
               if(Math.Abs(candidate) <=
                  min_candidate)
                   return;
                   
               if (is_target(p)) {
                   
                   Result=candidate;
                   
               } else {
                   
                 if (p > target) {
                   
                   calculate
                     (from,
                      candidate,
                      (from+candidate)/2.0M);
                     
                 } else if (p < target) {
                   
                   calculate
                     (candidate,
                      to,
                      (to+candidate)/2.0M);
                 }
                     
               }
            }
        }
        
        /*
         * Cube root is a specific case of
         * generic root.
         */
        class CubeRoot<T> : Root<T>
        {
            public CubeRoot(T target) :
                 base(3,target) {}
        }
    
        /*
         * main program
         */
        static void Main(string[] args)
        {
           decimal number =
               Convert.ToDecimal
                 (Console.ReadLine());
           
           CubeRoot<decimal> cubeRoot=
               new CubeRoot<decimal>(number);
           
           Console.WriteLine
             ("\nCube root of {0} is {1}",
             number,
             cubeRoot);
        }
    }
}

4. By โ€ŽDael Winograd

Made by โ€ŽDael Winograd. Simple Program to find Cube root. Source

6
??6 = 1.8171
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Init(ref double min, ref double max, double num)
        {
            if (num <= 1)
            {
                min = num;
                max = 1;
            }
            else
            {
                min = 0;
                max = num;
            }
        }
        
        static double Search(double min, double max, double num)
        {
            double avg = (min+max)/2;
            if (Math.Abs(num - avg*avg*avg) < 0.00001)
            {
                return avg;
            }
            else
            {
                if (avg*avg*avg > num)
                {
                    max = avg;
                }
                else
                {
                    min = avg;
                }
            }
            return Search(min, max, num);
        }
        
        static void Main(string[] args)
        {
            double num = double.Parse(Console.ReadLine());
            bool minus = num<0;
            num = Math.Abs(num);
            double min=0, max=0;
            Init(ref min, ref max, num);
            double r = Search(min, max, num);
            r = Math.Round(r, 4);
            if (minus)
            {
                num *= -1;
                r *= -1;
            }
            Console.WriteLine("ยณโˆš{0} = {1}", num, r);
        }
    }
}

5. By Mosi B

Made by Mosi B. Source

8
Cube-Root for 8 is: 2
using System;
namespace SoloLearn
{
    class Program
    {
        static void Main()
        {
            double i = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine($"Cube-Root for {i} is: {CubeRoot(i)}");
        }
        static double CubeRoot(double x)
        {
            double p = 1.0/3.0;
            double res = Math.Pow(x, p);
            return res;
        }
    }
}

6. By Leonid Zazulin

Made by Leonid Zazulin. A single line C# program to find cube root. Source

16
2.51984209978975
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine(Math.Pow(Convert.ToDouble(Console.ReadLine()),1.0/3.0));
		}
	}
}

7. By Antonio Wilson

Made by Antonio Wilson. Source

Cube root of 15: 2.4662
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
			double number = Double.Parse(Console.ReadLine());
			Console.WriteLine("Cube root of " + number + ": " + Math.Round(FindCubeRoot(number, number), 4));
		}
		
		static double FindCubeRoot(double number, double estimate) {
		    double guess = estimate - (estimate * estimate * estimate - number) / (3 * estimate * estimate);
		    if (Math.Round(guess, 8) == Math.Round(estimate, 8)) {
		        return guess;
		    }
		    return FindCubeRoot(number, guess);
		}
	}
}

8. By Mj

Made by Mj. Source

Enter the number for which you want the cube root: 9
The cube root of 9 is 2.08008
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static double abs (double num){if (num>=0) return num; else return -num;}
        
        static bool inverse (double num, 
        ref double answer,
        Func <double, double> function,         
        double first=1.0, double prec=1e-5,
        double step=0.1, int maxiter=1000)
        {
            double attempt=first;
            double diff;
            double adjust;
            int iterations=0;
            do {
                diff=num-function(attempt);
                attempt+=(adjust=step*diff/(function(attempt+step)-function(attempt)));
            } while  (abs(adjust)>prec&&++iterations<maxiter);
            
            answer=attempt;
            return (iterations<maxiter);
        }
        
        static double cube (double num)
        {
            return num*num*num;
        }
        
        static void Main(string[] args)
        {
            bool success=false;
            double input=27;
            while (!success){
            try{
            Console.WriteLine("Enter the number for which you want the cube root:");
             input=Convert.ToDouble(Console.ReadLine());
            success=true;
            }
            catch (Exception e){
                Console.WriteLine("The input must be a number. Try again.");
            }
            
            }
            double answer=0;
            if (inverse(input,ref answer,cube)){
            Console.WriteLine("The cube root of {0} is {1:F5}",input,answer);}
            else {
                Console.WriteLine("Max iterations exceeded. The inverse could not be found.");
            }
        }
    }
}

9. By Slavik T

Made by Slavik T. Simple C Sharp program for finding the cube root of a input number. Source

Enter number: 7
Answer: Cube root = 1.9129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
	class Program
	{
		static void Main(string[] args)
		{
			int a  ;

           Console.Write("Enter number: ");

           a = Convert.ToInt32(Console.ReadLine());
           
           Console.WriteLine(a);



        double b = Math.Pow(a, 1 / 3d);

            Console.WriteLine(" Answer: Cube root = {0:F4}", b );

                Console.ReadLine();
			
		}
	}
}

10. By Weng Shaokai

Made by Weng Shaokai. Source

12
2.2894
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                double x = double.Parse(Console.ReadLine());                                               
                Console.WriteLine(cal(1,x,1));              
            } 
            catch
            {
                Console.WriteLine("Input Error(Please input Number)");               
            }    
        }
        
        public static double cal(double Ans,double input,double plax)
        {
            double plax0 = plax;
            if (plax0<0.0001)
            {
                return Ans;
            }
            else
            {
                for (;;Ans += plax)
                {
                    double Ans_3 = Ans*Ans*Ans;
                    if(Ans_3 == input)
                    {
                        return Ans;
                    }
                    else if(Ans_3 > input)
                    {
                        return cal(Ans-plax0,input,plax0*0.1);
                    }
                }
            }        
        }
    }
}

11. By Arthur Bikmullin

Made by Arthur Bikmullin. Source

5
Cube root of 5 is 1.71
using System;

namespace CubeRoot {
	class CubeRoot {
		static float Difference(float num, float mid) {
			if (mid * mid * mid > num) return mid * mid * mid - num;
			return num - mid * mid * mid;
		}

		static float FindCubeRoot (float num) {
			float start = 0, end = num, e = 0.0001f;

			while (true) {
				float mid = (start + end) / 2;
				float error = Difference(num, mid);

				if (error <= e) return mid;
				else if (mid * mid * mid > num) end = mid;
				else start = mid;
			}
		}

		static void Main (String[] args) {
			float num = Convert.ToSingle(Console.ReadLine());
			Console.WriteLine($"Cube root of {num} is {Math.Round(FindCubeRoot(num), 4)}");
		}
	}
}