+ 1
"That's Odd..." Problem using c#
https://www.sololearn.com/coach/63?ref=app I'm not sure why this isn't working it seems to work for tests 2 and 4. However, test 2 has only two numbers (the list length, 1 , and an even number). I'm not sure what test 4 is because I need pro
5 odpowiedzi
+ 5
Riley ,
the logic of the input should be: (task description says: first input denotes the number of inputs, the folling innputs has to be taken one at a time)
> take first input (`count` of values)
> use a *for loop* to run until `count` inputs are taken
use a variable that can hold the running sum like `sum`
read input and check if it is an even number
if yes => add it to `sum`
if no => no action required
> when the loop is terminated, `sum` holds the result
+ 1
Riley the hidden test cases are hidden for everyone. Having Pro won't reveal them.
The issue I see is the program reads only the first number after reading N. Code Coach provides the remaining numbers on separate input lines. The program should loop N times, reading the numbers within the loop.
+ 1
Brian rian I've also tried that, but it still only completes the 2nd and 4th.
Im pretty sure that it's only reading the first 2 numbers in the tests.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sololearn
{
class Program
{
static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine());
int c = 0;
int sum = 0;
if(c < N)
{
c++;
int num = Convert.ToInt32(Console.ReadLine());
if(num%2==0 && num!=0)
{
sum+=num;
}
}
Console.WriteLine(sum);
}
}
}
+ 1
Riley the second code that you posted is better, but it needs to loop. It would pass all tests if it would use a while statement instead of a simple if statement.
0
Here is my current code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sololearn
{
class Program
{
static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine());
string[] numbers = Console.ReadLine().Split();
int sum = 0;
foreach (string a in numbers)
{
int b = Convert.ToInt32(a);
if (b%2==0 && b!=0)
{
sum+=b;
}
}
Console.WriteLine(sum);
}
}
}