Hello all,
I'm fairly inexperienced at C# and I am currently working on a project that requires me to generate two random numbers, then apply a random mathematical operator (+,-,*,/). I'm currently struggling on how I would do this. So far I have worked out how to generate two random numbers (not too difficult) but i've looked everywhere on how to apply a random maths operator to it.

I was thinking perhaps storing the operators in an array, and applying it to the numbers that way, again using a randomize feature. Am i pointing myself in the right direction?

Thanks in advance.

7 Years
Discussion Span
Last Post by NOLK

You could try something simple like

int mathOperator = random.Next(1, 5); // gives value of 1-4
switch (mathOperator)
    case 1:
        result = leftSide + rightSize;
    case 2:
        result = leftSide - rightSize;
    case 3:
        result = leftSide * rightSide;
    case 4:
        result = leftSide / rightSize;
Votes + Comments
Has to be no more than that!

Thanks for the reply Apegram. I'm still a little confused about the code you've suggested. As far as I can make out, you've proposed a switch. However, how would the application randomly pick one of the case's? The random.next function is only for generating to two random numbers between 1 - 5 right?

If i'm wrong and it does randomize, how could i go about feeding the two numbers i have from my integer variables into that code?

Kind regards,


This is code that would follow the generation of your other two random numbers. In my example, I've called the variables holding those values leftSide and rightSide.

random.Next(1, 5) is an overload that will pull a value greater than or equal to 1 and less than 5 (not equal), meaning it's valid range is 1 through 4. You generate this number, and then the switch statement controls the math operation. If you randomly get a 2, you subtract. If you randomly get a 4, you divide.


Excellent. Thank you very much, I understand now how it works. How would i go about getting the switch to display something on screen? Is it "return XXXX;"? Ive typed that in but i keep getting the error 'keyword must not be followed by an object expression.'

I'm quite new to a switch working like this.


Well, if you are using his switch and you are using a console, then the command to print something on the screen would be:


If you are using a windows form, then choose the label that you made (you must make a label or a textfield), and then set componnentNamehere.Text = result.ToString();

I am taking it that you are VERY new to C#, I would suggest learning input output before going into while and switch, you must be racing ahead of yourself if you do not know printing things on the screen yet. But I wish you best of luck, until the next question :D. Have a nice day :d.


Lol Wargrider, you'd be surprised. I know i sound like a total beginner, but i've been studing c# for about 8 months! I'm just totally incapable at programming that's all. :)

Thanks for all the help though!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.