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: