Math Operations With Logical Operation Using C#

I have shown how you can perform math as well as logical operations together.

So first we will create a new windows application and design the below form.


Now use the below code,

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel;    
  4. using System.Data;    
  5. using System.Drawing;    
  6. using System.Linq;    
  7. using System.Text;    
  8. using System.Threading.Tasks;    
  9. using System.Windows.Forms;    
  10.     
  11. namespace Example_of_math_variables_and_if_statements    
  12. {    
  13.     public partial class Form1 : Form    
  14.     {    
  15.         private string x; //This defines the string variable x    
  16.         private string y;    
  17.         private string random_string;    
  18.         private int x_number; //This defines the integer variable x_number    
  19.         private int y_number;    
  20.         private int random;    
  21.         public Form1()    
  22.         {    
  23.             InitializeComponent(); //This creates the window from the information that was entered in the form designer    
  24.         }    
  25.         private void radioButton1_CheckedChanged(object sender, EventArgs e)    
  26.         {    
  27.             x = this.textBox1.Text;    
  28.             y = this.textBox2.Text;    
  29.             if (x == ""//This sees if x has no value    
  30.             {    
  31.                 x = "0"//This sets x to 0    
  32.             }    
  33.             if (y == ""//This sees if x has no value    
  34.             {    
  35.                 y = "0"//This sets y to 0    
  36.             }    
  37.             x_number = Convert.ToInt32(x); //This converts the string x to a integer then stores it in x_number    
  38.             y_number = Convert.ToInt32(y);    
  39.             random = x_number + y_number; //This sets the integer variable random to the value of x_number plus y_number    
  40.             random_string = Convert.ToString(random); //This converts the integer random to the string random_string    
  41.             this.label3.Text = random_string;    
  42.         } //This method is called when the radiobutton (a button that when pressed makes every other radiobutton in its groupbox not pressed) is changed    
  43.         private void radioButton2_CheckedChanged(object sender, EventArgs e)    
  44.         {    
  45.             x = this.textBox1.Text;    
  46.             y = this.textBox2.Text;    
  47.             if (x == "")    
  48.             {    
  49.                 x = "0";    
  50.             }    
  51.             if (y == "")    
  52.             {    
  53.                 y = "0";    
  54.             }    
  55.             x_number = Convert.ToInt32(x);    
  56.             y_number = Convert.ToInt32(y);    
  57.             random = x_number - y_number;    
  58.             random_string = Convert.ToString(random);    
  59.             this.label3.Text = random_string;    
  60.         }    
  61.         private void radioButton4_CheckedChanged(object sender, EventArgs e)    
  62.         {    
  63.             x = this.textBox1.Text;    
  64.             y = this.textBox2.Text;    
  65.             if (x == "")    
  66.             {    
  67.                 x = " 0";    
  68.             }    
  69.             if (y == "")    
  70.             {    
  71.                 y = "0";    
  72.             }    
  73.             x_number = Convert.ToInt32(x);    
  74.             y_number = Convert.ToInt32(y);    
  75.             random = x_number * y_number;    
  76.             random_string = Convert.ToString(random);    
  77.             this.label3.Text = random_string;    
  78.         }    
  79.         private void radioButton3_CheckedChanged(object sender, EventArgs e)    
  80.         {    
  81.             x = this.textBox1.Text;    
  82.             y = this.textBox2.Text;    
  83.             if (x == "")    
  84.             {    
  85.                 x = "0";    
  86.             }    
  87.             if (y == "")    
  88.             {    
  89.                 y = "0";    
  90.             }    
  91.             x_number = Convert.ToInt32(x);    
  92.             y_number = Convert.ToInt32(y);    
  93.             if (y_number == 0) //This is to make sure that we don't divide by 0    
  94.             {    
  95.                 random_string = "Divide by zero error";    
  96.             }    
  97.             else    
  98.             {    
  99.                 random = x_number / y_number;    
  100.                 random_string = Convert.ToString(random);    
  101.             }    
  102.             this.label3.Text = random_string;    
  103.         }    
  104.         private void radioButton7_CheckedChanged(object sender, EventArgs e)    
  105.         {    
  106.             x = this.textBox1.Text;    
  107.             y = this.textBox2.Text;    
  108.             if (x == "")    
  109.             {    
  110.                 x = "0";    
  111.             }    
  112.             if (y == "")    
  113.             {    
  114.                 y = "0";    
  115.             }    
  116.             x_number = Convert.ToInt32(x);    
  117.             y_number = Convert.ToInt32(y);    
  118.             if (x_number == y_number) //This checks if the integer x_number is equal to the integer y_number    
  119.             {    
  120.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.yes;   //If it is the picture is set to a check    
  121.             }    
  122.             else //If they are not then the picture is set as an X    
  123.             {    
  124.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.no;    
  125.             }    
  126.         }    
  127.         private void radioButton8_CheckedChanged(object sender, EventArgs e)    
  128.         {    
  129.             x = this.textBox1.Text;    
  130.             y = this.textBox2.Text;    
  131.             if (x == "")    
  132.             {    
  133.                 x = "0";    
  134.             }    
  135.             if (y == "")    
  136.             {    
  137.                 y = "0";    
  138.             }    
  139.             x_number = Convert.ToInt32(x);    
  140.             y_number = Convert.ToInt32(y);    
  141.             if (x_number > y_number) //Check if the integer x_number is greater than integer y_number    
  142.             {    
  143.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.yes;    
  144.             }    
  145.             else    
  146.             {    
  147.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.no;    
  148.             }    
  149.         }    
  150.         private void radioButton6_CheckedChanged(object sender, EventArgs e)    
  151.         {    
  152.             x = this.textBox1.Text;    
  153.             y = this.textBox2.Text;    
  154.             if (x == "")    
  155.             {    
  156.                 x = "0";    
  157.             }    
  158.             if (y == "")    
  159.             {    
  160.                 y = "0";    
  161.             }    
  162.             x_number = Convert.ToInt32(x);    
  163.             y_number = Convert.ToInt32(y);    
  164.             if (x_number < y_number) //Check if the integer x_number is less than integer y_number    
  165.             {    
  166.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.yes;    
  167.             }    
  168.             else    
  169.             {    
  170.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.no;    
  171.             }    
  172.         }    
  173.         private void radioButton5_CheckedChanged(object sender, EventArgs e)    
  174.         {    
  175.             x = this.textBox1.Text;    
  176.             y = this.textBox2.Text;    
  177.             if (x == "")    
  178.             {    
  179.                 x = "0";    
  180.             }    
  181.             if (y == "")    
  182.             {    
  183.                 y = "0";    
  184.             }    
  185.             x_number = Convert.ToInt32(x);    
  186.             y_number = Convert.ToInt32(y);    
  187.             if (x_number >= y_number) //Check if the integer x_number is greater than or equal to integer y_number    
  188.             {    
  189.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.yes;    
  190.             }    
  191.             else    
  192.             {    
  193.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.no;    
  194.             }    
  195.         }    
  196.         private void radioButton9_CheckedChanged(object sender, EventArgs e)    
  197.         {    
  198.             x = this.textBox1.Text;    
  199.             y = this.textBox2.Text;    
  200.             if (x == "")    
  201.             {    
  202.                 x = "0";    
  203.             }    
  204.             if (y == "")    
  205.             {    
  206.                 y = "0";    
  207.             }    
  208.             x_number = Convert.ToInt32(x);    
  209.             y_number = Convert.ToInt32(y);    
  210.             if (x_number <= y_number) //Check if the integer x_number is less than or equal to integer y_number    
  211.             {    
  212.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.yes;    
  213.             }    
  214.             else    
  215.             {    
  216.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.no;    
  217.             }    
  218.         }    
  219.         private void radioButton10_CheckedChanged(object sender, EventArgs e)    
  220.         {    
  221.             x = this.textBox1.Text;    
  222.             y = this.textBox2.Text;    
  223.             if (x == "")    
  224.             {    
  225.                 x = "0";    
  226.             }    
  227.             if (y == "")    
  228.             {    
  229.                 y = "0";    
  230.             }    
  231.             x_number = Convert.ToInt32(x);    
  232.             y_number = Convert.ToInt32(y);    
  233.             if (x_number != y_number) //Check if the integer x_number is not equal to integer y_number    
  234.             {    
  235.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.yes;    
  236.             }    
  237.             else    
  238.             {    
  239.                 this.pictureBox1.Image = global::Example_of_math_variables_and_if_statements.Properties.Resources.no;    
  240.             }    
  241.         }    
  242.     }    
  243. }    
In the above code I have performed all the math as well as logical operations.

 

Ebook Download
View all
Learn
View all