Skip to content

C # "Math" Program modified according to suggestion

Asked Mar 19, 2007, 6:26 AM EDT2 answers
Share
LinkedInX

I modified C# "Math" Program  according to suggestion but it is not working. Please correct it.

If one entered a number, squre root of that number should be the output. That means for example: If I entered 10 as a input, output should be 3.1622.
 
square root of 10 = 3.1622.

using System;

public class Math

{

public static void Main()

{

double sqrt, number;

string sNum;

Console.Write("Number = ");

sNum = Console.ReadLine();

number = Convert.ToDouble(sNum);

sqrt = SqrtRoot(number);

Console.WriteLine("Square Root of the number = {0}", sqrt);

}

public static double SqrtRoot(double num)

{

double sRoot;

sRoot = Math.Sqrt(num);

return sRoot;

}

}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    you need a ReadLine before the end of main so it doesn't exit before you see the output.  Here is the solution:

    Best,

    Mike G.

    C#
    using System;
    public class Math
    {
    public static void Main()
    {
    double sqrt, number;
    string sNum;
    Console.Write("Number = ");
    sNum = Console.ReadLine();
    number = Convert.ToDouble(sNum);
    sqrt = SqrtRoot(number);
    Console.WriteLine("Square Root of the number = {0}", sqrt);
    Console.ReadLine();
    }
    public static double SqrtRoot(double num)
    {
    return System.Math.Sqrt(num);
    }
    }
    0
  • 0
    Thank you for your help, Maha
     
    not that Console.ReadLine(); but it is System.Math.Sqrt(num); correcting the program.
    0