0

I wrote a simple programme in C# that calculate the power of a number when you give a X and Y (X^Y). The answer is showed in a textbox. For example : X=2 and Y=5 the answer in the textbox is:

2 X 2 = 32
2 X 2 = 32
2 X 2 = 32
2 X 2 = 32
2 X 2 = 32
2 X 2 = 32

My question is how can I do to have this result:

2 X 2 = 2
2 X 2 X 2 = 8
2 X 2 X 2 X 2 = 16 
2 X 2 X 2 X 2 X 2 = 32

Here is my code:

private void button1_Click (object sender, EventArgs e)
{
    double number1;
    doubLe number2;
    int count = 0;

    double power;
    number1 = double.Parse(Txtnumber1.Text};
    number2 = double.Parse(Txtnumber2.Text};

    while (count < number2)
    {
        power = Math.Pow(number1, number2);
        Txtanswer.Text = Txtanswer.Text + number1.ToString() + " " + " x" + " " + number1.ToString() + " " + "=" + power.ToString() + "\r\n";
        count += 1;
    }
}

Number1 is X.

Number2 is Y

  • 1
    Please, provide code as text not as picture. – George Alexandria Sep 24 '17 at 14:09
  • you have a typo when you're trying to print the second number and reprinting the first. – kenny Sep 24 '17 at 14:11
  • 2
    An example where the original value is something *other* than 1 would be a lot easier to understand. – Jon Skeet Sep 24 '17 at 14:22
  • You may want to take a look at this: https://stackoverflow.com/questions/3754582/is-there-an-easy-way-to-return-a-string-repeated-x-number-of-times. Also, consider using a `for` rather than `while` loop for scenarios where you need to loop a known number of times. – JohnLBevan Sep 24 '17 at 14:23
  • What is supposed output for Y being 3.7? – Antonín Lejsek Sep 24 '17 at 14:34
  • I'm trying with natural numbers. –  Sep 24 '17 at 14:42
  • @JohnLBevan Yes, but I wanted to try with a while loop. –  Sep 24 '17 at 14:47
  • 2
    Okay, so I'd personally *start* by reducing this to a console application. Get rid of the GUI side, hard-code the inputs, and just write out the lines. When you've got that working, migrating it to a GUI is a separate step. – Jon Skeet Sep 24 '17 at 15:04
  • 1
    Next hint: look at what your loop body does. It doesn't use the value of `count` at all except to increment it, so each iteration *will* produce the same output. – Jon Skeet Sep 24 '17 at 15:05

1 Answers1

0

needs lots of improve but something like that ?

class PowBuilder
{
    public static string PrintNumber(double number, double times)
    {
        string temp = "";
        for(int i = 1;i < times; i++)
        {
            temp += number.ToString() + " x ";
        }
        return temp;
    }

}
class Program
{
    private static string PowFan(double a, double b)
    {

        int count = 1;
        double power;
        string sb = "";

        while (count < b)
        {
            power = Math.Pow(a, count);
            sb += PowBuilder.PrintNumber(a, count) + a.ToString() + " = " + power.ToString() + " \r\n";
            count += 1;
        }
        return sb;
    }
        static void Main(string[] args)
    {
        Console.WriteLine(PowFan(2, 15).ToString());
    }
}

//output
//2 = 2
//2 x 2 = 4
//2 x 2 x 2 = 8
//2 x 2 x 2 x 2 = 16
//2 x 2 x 2 x 2 x 2 = 32
//2 x 2 x 2 x 2 x 2 x 2 = 64
//2 x 2 x 2 x 2 x 2 x 2 x 2 = 128
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 256
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 512
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 1024
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 2048
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 4096
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 8192
//2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 16384
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43