Hi
I'm a beginer programer and I need your help
I'm using visual studio 2005 writing with c#, Iwant to make a small program at console
this program give the root of a number.
I writ this code

double one = double.Parse(Console.ReadLine());
            for (double x = 1; x <= one; x++)
            {
                double result = one / x;
                if (result == x)
                {
                    Console.WriteLine(result);
                }

by this way it will give you the root but if it is a real number like (25-it's root 5)
when i make x increase by0.1 the code didn't do any thing the code will be

double one = double.Parse(Console.ReadLine());
            for (double x = 1; x <= one; x+=0.1)
            {
                double result = one / x;
                if (result == x)
                {
                    Console.WriteLine(result);
                }

I want to know why

any help please

if i'm not mistaken a for loop can not increase by a decimal number. I think it can only increase by a whole number such as 1, 2, 3, etc...

if i'm not mistaken a for loop can not increase by a decimal number. I think it can only increase by a whole number such as 1, 2, 3, etc...

Thank you for your reply so how can I loop by decimal number and if you run my code without if condition it will run the program
and is thier any other method to calculate the root of a number
thank you for your assistance

Actually there is a built in Class that has a method for it.

Math.Sqrt();

so in your case it would look something like this.

double one = double.Parse(Console.ReadLine());

double result = Math.Sqrt(one);

Console.WriteLine(result);

I forgot, the Math class also has many other methods. You would probably have to look them up if you arent using an IDE that lists them.

Hi
I'm a beginer programer and I need your help
I'm using visual studio 2005 writing with c#, Iwant to make a small program at console
this program give the root of a number.
I writ this code

For really understanding how the computer really does it, you need to understand how they did it in the old days, take a look here, you may be able to extract something that will work. Depending upon your acceptance of a margin of error, it can be quite simple.

http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

-George

I just couldn't let it go so
It is pretty sloppy, but gets the job done... cleaning up the code is not something I'm into right now(tired)... but I think it is pretty clear
N is the nearest root less than the needed one
d is the distance that N*N makes from the number you're looking for a root to.
n is iterations....
the rest... it works, just not as clean as I like to be. The commented out code is just in case you want to see the steps of each section. I ran through the derivatives and arrived to the same thing at Wiki, so here goes Modify as you need for command line, but so you know, I have a form that prompts for number you want root to, iterations you want to run for accuracy ~35 or more creates NaN and then a calculate button.

namespace WindowsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
      Double answer;
      Double S;
      S=Convert.ToDouble(input.Text);
      int t = Convert.ToInt16(nTotal.Text);
      answer=0;
      int N=0;
      do{
        N++;
      }while(N*N<S);
      N--;
      int x = Convert.ToInt32(S);
      int d=(x-N*N);
      // get ready cuz it gets ugly!
      for (int n=0;n<t;n++){ // 10 iterations should be scary accurate
        double calculations=(Math.Pow((-1),n)*factorial(2*n)*Math.Pow(d,n))/((1-2*n)*Math.Pow(factorial(n),2)*Math.Pow(4,n)*Math.Pow(N,(2*n-1)));
        answer = answer + calculations;
        /*int a = Math.Pow(-1,n);
        int b = factorial(2*n);
        int c = Math.Pow(d,n);
        int g = (1 - 2 * n);
        int h = factorial(n) * factorial(n);
        int i = Math.Pow(4 , n);
        int j = Math.Pow(N ^ (2 * n - 1));
        answer = answer + ((a * b * c) / (g * h * i * j));
        }*/
        string strAnswer = Convert.ToString(answer);
        output.Text = strAnswer;
      }
    }

    int factorial(int number){
      if (number==0)
        return 1;
      int l=number;
      for (int z=1;z<number;z++){
        l=l*z;
      }
      return l;
    }
  }
}

Change
while(N*N<S);
to
while(N*N<=S);

you'll get 5 for sqrt(25) not 4.99999...
I didn't realize it until after I posted that it did something stupid to numbers with easy sqrt

This algorithm loses accuracy after 3 iterations... there is something wrong in it... I am going over it again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.