Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all. I am currently in a Fundamental Programming and Logic class. My project is creating a simple calculator. I have everything written except for the modulus button. I have searched the web trying to find how to code this but am unable to find exactly what I am needing. I understand that modulus finds the remainder between two numbers. What I am trying to do is take the first number entered into the txtbox, hit the modulus button then enter the second number and hit equal. I am lost on how to code this. Please teach me something, oh gurus of code. I'm 99% done with this project.

Thank you all, ahead of time, for taking time to answer.
Posted
Comments
Richard C Bishop 5-Mar-13 14:52pm    
Just extract the users input and use the modulus operator.

Convert.ToInt32(txtbox1.Text) % Convert.ToInt32(txtbox2.Text)
tbonvillain 5-Mar-13 15:09pm    
Is this if I'm using two text boxes? I'm only using one text box for the project.
Richard C Bishop 5-Mar-13 15:12pm    
Ok, so just use the Split() method that reurns a string array and check for the modulus symbol. If it is present then perform the calculation on the two numbers that were entered. You will be using the string array to get your values for that.
tbonvillain 5-Mar-13 15:13pm    
Thank you. I'll give it a shot.
Richard C Bishop 5-Mar-13 15:30pm    
Check my solution, it works.

1 solution

Here is a working example of what you need:

string[] s = TextBox1.Text.ToString().Split('%');
       int n = 0;
       if (TextBox1.Text.ToString().Contains("%"))
       {

           n = Convert.ToInt32(s[0].ToString()) % Convert.ToInt32(s[1].ToString());
       }
       Label1.Text = n.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900