Saturday, 24 November 2012

C#(Sharp) Code to Make Simple Calculator -Program

In C# we can make a simple Calculator within an hour.Simple limited number of lines of code is needed for this. A sample design with program(source code ) is here.Addition,Multiplication,Subtraction and Division can be done using this calculator.

a sample design of a simple calculator form

In this the form have a TextBox for inputting and outputting the data.The name of the TextBox  is changed to 'txtNumber' ,the form have 17 buttons for different uses.drag and drop a button to the form ,change the text property as '7' this button for print 7 on the TextBox,change the button controls name property as 'btn7'.like this add 9 button controls to form for 0-9 numbers also change the name property of all buttons like 'btn7'.Now we need 5 more buttons for arithmetic operation like '+','-','*, '=' and '/'  so drag and drop 5 more buttons and change their names 'btnAdd','btnSub',btnMul','btnresult' and 'btndivision' respectively .Finally we want 2 more buttons for "clear" and "clear all" operation in the above form these are represented by button 'C' and 'CA' respectively also changes the names of these buttons as 'btnClear' and 'btnClearAll'.Now we are completed the design part of the form. After completing the design we can start the coding .for coding open the code page of the(double click the form) form and write the code here is the code for simple calculator:

The code :

public partial class Calculator : Form  //'Calculator' is my forms name you just  replace this with your //form name

    {
        decimal  FirstValue;
        string  Operation;
    
        public Calculator()
        {
            InitializeComponent();
            txtNumber.Focus();
        }

        private void Calculator_Load(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FirstValue=Convert.ToDecimal   (txtNumber.Text );
            Operation = "ADD";
            txtNumber.Clear ();
            txtNumber.Focus();
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {
            if (Operation == "ADD")
            {
                txtNumber.Text=((FirstValue)+(Convert.ToDecimal(txtNumber.Text ))).ToString();
            }

            else if (Operation == "SUB")
            {
               txtNumber.Text = (FirstValue - Convert.ToDecimal(txtNumber.Text)).ToString();
            }

            else if (Operation == "DIVIDE")
            {
                txtNumber.Text = (FirstValue / Convert.ToDecimal(txtNumber.Text)).ToString();
            }

            else if (Operation == "MUL")
            {
                txtNumber.Text = (FirstValue * Convert.ToDecimal(txtNumber.Text)).ToString();
            }
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            FirstValue = Convert.ToDecimal(txtNumber.Text);
            Operation = "SUB";
            txtNumber.Clear();
            txtNumber.Focus();
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            FirstValue = Convert.ToDecimal(txtNumber.Text);
            Operation = "DIVIDE";
            txtNumber.Clear();
            txtNumber.Focus();
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            FirstValue = Convert.ToDecimal(txtNumber.Text);
            Operation = "MUL";
            txtNumber.Clear();
            txtNumber.Focus();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtNumber.Clear();
            txtNumber.Focus();
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            txtNumber.Clear();
            FirstValue = 0;
            txtNumber.Focus();     
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 7;
            txtNumber.Focus();
            int length=  txtNumber.Text.Length;
            txtNumber.Select(length, length);  
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 8;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 9;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 4;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 5;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 6;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 3;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 2;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 1;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            txtNumber.Text = txtNumber.Text + 0;
            txtNumber.Focus();

            int length = txtNumber.Text.Length;
            txtNumber.Select(length, length);
        }
    }
}


after completing the coding run the form 
















Tuesday, 30 October 2012

C# Code check the given number is Positive or negative

      

      C# Code to Check the given number is Positive(+ve) or                                         

                                   negative(-ve)

A simple if condition makes it possible.Checking the given number is +ve or -ve is have only 3 to 6 lines of simple code. It is very easy to understand.Firstly create a new project or a website.the new form must have at least a Text box (for inputting the value),a submit button(to check the number -ve or not by clicking  the button) finally another Text box or a label for output the result.My final form looks like this.

Design for the program 

This form have One Text box for inputting the checking value named as "txtNumber",a submit button to execute the code for checking the number is positive or negative named as btnCheck  and the output will be printed in alabel named as lblResult the labels forecolor set as red

after creating the form just double click the button and write this code:

             private void btnCheck_Click(object sender, EventArgs e)
        {
            int inputNumber; //inyeger variable for storing the inputted value
             
            inputNumber = Convert.ToInt32 (txtNumber.Text ); //assign the inputted value to the    
                                                                                                   // variable'inputNumber'

            if (inputNumber >= 0)     // if the given number is greater than zero then the number is    
                                                     // positive
            {
                lblResult.Text = "The Nunber is Positive";  //print output in labelbox 'lblResult'
            }
            else  //else the number is negative
            {
                lblResult.Text = "The Number is Negative";  //print output in labelbox 'lblResult'
            }



C# Code to find Sum of N Natural Numbers


                C# Code to find Sum of N Natural Numbers

Finding Sum of N natural numbers in C# is very simple. it have simple 3 to 6 lines of codes .we have to input the limit from a textbox and output the sum  to a second textbox .so create a new project or website in visual studio and place 2 textboxes,2 labels and a submit button .change the first textboxes name as txtLimit ,change the 2nd textboxes name as txtSumOFNaturalNumbers and change buttons name as btnSum 
set the labels name as suitable my form design is like this

The first text box accpts the limit and the second textbox is outputs the sum of n natural numbers after clicking the sum button


after creating the design double click the button((btnSum) and write the code below:

        private void btnSum_Click(object sender, EventArgs e)
        {
            int numberCount, limit,sum=0;

            limit = Convert.ToInt32(txtLimit.Text );     // inputted limit value is assigned to integer variable 'limit'
                    //(the actual data type of inputted textbox value is string so it is converted into integer)

            for (numberCount = 0; numberCount <= limit; numberCount++) //first number is started with '0' end    
                with 'limit'
              {
                 sum=sum + numberCount;   //find sum 
              }

            txtSumOFNaturalNumbers.Text = sum.ToString();




C# code to find Sum And Average of 3 Numbers

C# Code to find Sum And Average of 3 Numbers


It is easy to find Sum and Average of 3 numbers in C# .Firstly create a new project in visual studio with C# Code .Open the form and drag and drop 5 text boxes , 3 for inputting 3 numbers and 2 for display sum and average.Also we need 5 labels for labeling 5 text boxes.
insert these 5 text boxes ,5 labels and a button. after inserting these controls select the first textbox and set the name as txtFirstNumber,select the second textbox and set the name as txtSecondNumber ,select the third textbox and set the name as txtThirdNumber,select the 4th  textbox and set the name as txtSum,select the 5th textbox and set the name as txtAverage and select the button and set the name as btnSumAndAverage.set the labels Text as appropriate name.after completing design the form is like this

The final design with a sample result values.


double click on the button and write the code below:



        private void btnSumAndAverage_Click(object sender, EventArgs e)
        {
            txtSum.Text = (Convert.ToInt32(txtFirstNumber.Text) + Convert.ToInt32(txtSecondNumber.Text)     
           + Convert.ToInt32(txtThirdNumber.Text)).ToString();
            txtAverage.Text = (Convert.ToInt32(txtSum.Text) / 3).ToString();
        }

See also:
 

        Code for Find FACTORIAL of a Given Number From TextBox

      Code For Check The Given Number is +VE or -Ve

  

          

C# Code to find Factorial of a given number

C#.net Code to Find Factorial of a Given Number 


It is easy to find Factorial of given number in C#.net only 2 texboxes is needed for inputting and outputting
firstly create a new project in visual studio with C# code the form must have 2 textboxes,2 labels and a button. select the firt textbox(input textbox) set the name as txtNumber,select the second textbox(output textbox) set the name as  txtFactorial ,select the button and set the name as btnFactorial ,select the first label and set the text as Enter the Number,select the second label and set the text as Factorial after this we finishes the designing part  my form design is like this 



this is my forms design the factorial is printed in the 2nd textbox the first textbox will be the inputting texbox 


After creating the form double click the button and write the following codes

                 private void btnFactorial_Click(object sender, EventArgs e)
        {
            int count=1,factorial=1,number=1,factorialNumber;

            factorialNumber=Convert.ToInt32(txtNumber.Text);

            while (count<= factorialNumber )
            {
                factorial = factorial * number;
                count ++;
                number++;
            }

            txtFactorial.Text = factorial.ToString();
            

C# Code for sum of digits (asp.net)

C# Code to Find Sum of Digits 

Here is a Simple code to find sum of digits .it is easy and simple for find sum of digits of a given number we have a form and 2 TextBoxes ,2 Labels and a Button.name the first textbox as txtNumber,name the 2nd textbox as txtSumOFDigits and name the button as btnSumOFDigits and name labels Enter the Number,Sum Of Digits respectively after that the form like this


My form have One TextBox for inputting the number ,2nd textbox will be outputs the result(sum of digits)


 after creating the form double click the button and write the code :

               
                private void btnSumOFDigits_Click(object sender, EventArgs e)
        {
            int sum = 0,number;
            number= Convert.ToInt32(txtNumber.Text);  // the number to be find sum of digits is move to a    
             variable number
            while (number!= 0)      //while loop it executes until the condition is false
            {
                int reminder;
                number= Math.DivRem(number, 10, out reminder);   // in this code we can get reminder of the  
                number as reminder(variable) and now the number variable have the value without last number
               
                sum =sum+ reminder;
            }
         
            txtSumOFDigits.Text = sum.ToString();
        }
             

eg:if the number is 234 the reminder is 4 then the current sum is 4(sum=sum+reminder)
and the number updated as 23 without the reminder (number= Math.DivRem(number, 10, out reminder) that is the first number is divided by 10 and find reminder and save current value into variable number without reminder. the loop will executes untill the number is 0

see also:
     



    

Sunday, 30 September 2012

C#(Sharp) code to find sum of 2 numbers from 2 text boxes

C# Code for Find Sum Of 2  Numbers (textbox)

it is very easy find sum of two numbers in C# .for adding 2 numbers in C# we have 3 textBoxes,3 Labels
and a Button.Firstly create a new project or a website in visual studio .insert 3 TextBoxes,3 Labels and a Button.select the first TextBox and change the name to  textBoxFirstNumber,select the second TextBox and change the name to textBoxSecondNumber,select third TextBox and change the name to textBoxSum ,set labelboxes name as First Number,Second Number Sum approximately and set the name of Button as buttonFindSum.after that your form looks like this

this is my forms design in this the bottom textbox have the sum of 2 text boxes this will add by clicking sum button and print the result


after creating the page double click on Sum Button and type the following codes

C#shap Code

private void buttonFindSum_Click(object sender, EventArgs e)
        {
            int firstNumber, secondNumber, sum; // to save numbers 

            firstNumber = Convert.ToInt32(textBoxFirstNumber.Text); //saves the first number into the variable
            secondNumber=Convert.ToInt32(textBoxSecondNumbe.Text ); //saves the second number into a            
            variable

            sum = firstNumber + secondNumber; //add two numbers

             textBoxSum.Text = sum.ToString();  // stores the sum to the third textbox(result),the variable sum is   
            in the datatype integer so we have to convert integer to string because the TextBox only accepts      
            string       variables and data
        }

We can simply write it 

 private void buttonFindSum_Click(object sender, EventArgs e)
        {
            textBoxSum.Text  = (Convert.ToInt32(textBoxFirstNumber.Text ) +    
           Convert.ToInt32(textBoxSecondNumber.Text)).ToString();