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();
}
See also:
Code for Find FACTORIAL of a Given Number From TextBox
Code For Find SUM OF DIGITS of a Given Number
See also:
Code for Find FACTORIAL of a Given Number From TextBox
Code For Find SUM OF DIGITS of a Given Number

No comments:
Post a Comment