Home > Software design >  Sum range using loop, calculate sum of odd and even numbers
Sum range using loop, calculate sum of odd and even numbers

Time:11-25

Hi I am sick of searching I could not find the exact code for my question. I need to code the sum of the odd numbers from 1 to 100 and sum of the even numbers from 2 to 100. This is what i have so far.

Thank you so much

// 1) using for statement to Sum Up a Range of values using Interactive
Console.WriteLine(" Sum Up a Range of values entered by User ");
Console.WriteLine();

// 2) Declare the Variables to be used in the Project
string strFromNumber, strToNumber;
int fromNumber, toNumber;
int sum = 0;

int i, even = 0, odd = 0; 
int[] array = new int[10];

// 3) Prompt the User to Enter the From Number to Sum From
Console.Write("Enter the From Number to Sum From: ");
strFromNumber = Console.ReadLine();
fromNumber = Convert.ToInt32(strFromNumber);

// 4) Prompt the User to Enter the To Number to Sum To
Console.Write("Enter the To Number to Sum To: ");
strToNumber = Console.ReadLine();
toNumber = Convert.ToInt32(strToNumber);

// 5) Use for statement to Sum up the Range of Numbers
for (i = fromNumber; i <= toNumber;   i)
{
    sum  = i;
}

if //(array[i] % 2 == 0) //here if condition to check number
{ // is divided by 2 or not
    even = even   array[i]; //here sum of even numbers will be stored in even
}
else
{
    odd = odd   array[i]; //here sum of odd numbers will be stored in odd.
}

Console.WriteLine("The Sum of Values from {0} till {1} = {2}",
        fromNumber, toNumber, sum);
Console.ReadLine();

CodePudding user response:

There is no need to write the complex code which you have written.

Problem is to calculate the sum of arithmetic progression. The formula to find the sum of an arithmetic progression is Sn = n/2[2a (n − 1) × d] where, a = first term of arithmetic progression, n = number of terms in the arithmetic progression and d = common difference.

So in case of odd numbers its a = 1, n = 50 and d = 2 and in case of even numbers its a = 2, n = 50 and d = 2

and if you try to normalize these above formulas, it will be more easy based on your problem.

the sum of the first n odd numbers is Sn= n^2

the sum of the first n even numbers is n(n 1).

and obviously, it's very simple to loop from ( 1 to 99 with an increment of 2 ) and ( 2 to 100 with an increment of 2 )

CodePudding user response:

In the simplest case, you can try looping in fromNumber .. toNumber range while adding number either to even or to odd sum:

// long : since sum of int's can be large (beyond int.MaxValue) let's use long
long evenSum = 0;
long oddSum = 0;

for (int number = fromNumber; number <= toNumber;   number) {
  if (number % 2 == 0)
    evenSum  = number;
  else
    oddSum  = number;
}

Console.WriteLine($"The Sum of Values from {fromNumber} till {toNumber}");
Console.WriteLine($"is {evenSum   oddSum}: {evenSum} (even)   {oddSum} (odd).");

Note, that you can compute both sums in one go with a help of arithmetics progression:

private static (long even, long odd) ComputeSums(long from, long to) {
  if (to < from)
    return (0, 0); // Or throw ArgumentOutOfRangeException

  long total = (to   from) * (to - from   1) / 2;

  from = from / 2 * 2   1;
  to = (to   1) / 2 * 2 - 1;

  long odd = (to   from) / 2 * ((to - from) / 2   1);

  return (total - odd, odd);
}

Then

(long evenSum, long oddSum) = ComputeSums(fromNumber, toNumber);

Console.WriteLine($"The Sum of Values from {fromNumber} till {toNumber}");
Console.WriteLine($"is {evenSum   oddSum}: {evenSum} (even)   {oddSum} (odd).");

CodePudding user response:

From the code snippet you shared, it seems like the user gives the range on which the sum is calculated. Adding to @vivek-nuna's answer,

Let's say the sum of the first N odd numbers is given by, f(n) = n^2 and the sum of the first N even numbers is given by, g(n) = n(n 1). So the sum of odd numbers from (l, r) = f(r) - f(l - 1). Similarly, the sum of even numbers from (l, r) = g(r) - g(l - 1).

Hope this helps.

  • Related