C# Only. Create a console application that reads a sequence of integers (they ca
ID: 3753880 • Letter: C
Question
C# Only.
Create a console application that reads a sequence of integers (they can be positive, negative, or zero) from the input. Do not use built-in functions or arrays to implement the logic. Perform the following tasks in each iteration:
(1) The app will sum up sequential evens or sequential odds—whichever the sequence the input values are in. Code number%2==0 can be used to determine whether a number is even or not.In general, sequential evens are even numbers that are together, and vice versa.
(2) When the input changes from even to odd or from odd to even OR when the iteration has terminated with CTRL-Z as an input(when the ReadLine() method returns a null), the app will display the sum calculated in Task 1.
(3) The app will identify the largest sum of sequential odds as well as the largest sum of sequential evens.
(4) When the app reaches the end-of-line with CTRL-Z as an input(when the ReadLine() method returns a null), the app will terminate and display the positive difference between the largest sum of sequential odds and the largest sum of sequential evens, calculated in Task 3.
For example, the inputs 2, 4, 6, 8, 1, 3, 5, 7 have sequential evens of 2, 4, 6, 8 and sequential odds of 1, 3, 5, 7. When you enter a "1" here, the app will display "The sum is 20" because the input has been changed from even (8) to odd (1). In the end, the app will display "The sum is 16" and then "The positive difference between the largest sums is 4" because of 2+4+6+8 - (1+3+5+7).
Explanation / Answer
Screenshot
--------------------------------------------------------------
Program
using System;
namespace SequenceApp
{
class Program
{
static void Main(string[] args)
{
//Variables for input and calculation
string input;
int evenSum = 0, oddSum = 0, val = 0, counter = 1, counterNext = 1;
//User prompt for input
Console.WriteLine("Enter the sequence one by one(stop using ctrl-z):");
//Read input and check if you enter Ctrl-Z
while ((input = Console.ReadLine()) != null && input != "")
{
//change the entered string to integer
val = Convert.ToInt32(input);
//Check odd sequence or even sequence
if (val % 2 == 0 && oddSum!=0 && counter==1)
{
evenSum += val;
Console.WriteLine("Odd Sum= {0}", oddSum);
counter++;
}
//Otherwise find even sum
else if(val % 2 == 0 && counter>=1)
{
evenSum += val;
}
//If it is odd same check and calculation
else
{
if (val % 2 != 0 && evenSum != 0 && counterNext == 1)
{
oddSum += val;
Console.WriteLine("Even Sum= {0}", evenSum);
counterNext++;
}
else if(val % 2 != 0 && counterNext >= 1)
{
oddSum += val;
}
else
{
Console.WriteLine("Odd Sum= {0}", oddSum);
}
}
}
//Calculate largest difference
int diff = evenSum - oddSum;
if (diff > 0)
{
Console.WriteLine("Larget differnce= {0}", diff);
}
else
{
diff = diff * -1;
Console.WriteLine("Larget Sum= {0}", diff);
}
}
}
}
------------------------------------------
Output
Enter the sequence one by one(stop using ctrl-z):
2
4
6
8
1
Even Sum= 20
3
5
7
^Z
Larget differnce= 4
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.