Reputation: 47
I am trying to write a simple code, which will take a input from user and save it into number variable and then I am trying to find out if this number is even, or odd using condition operator, but I am bit stuck of how to use Console.WriteLine() in this condition operator and where to call it. Maybe someone can help me to understand it more clearly. Thanks in advance!
using System;
namespace ConditionalOperatorExercise
{
class Program
{
static void Main(string[] args)
{
int number;
number = Convert.ToInt32(Console.ReadLine());
var evenNumber = number % number == 1 ? Console.WriteLine("Number is even"); : Console.WriteLine("Number is odd");
}
}
}
Upvotes: 1
Views: 158
Reputation: 39956
You can change your code like this:
var message = number % 2 == 0 ? "Number is even" : "Number is odd";
Console.WriteLine(message);
Please note that you need to check the number % 2
is equal to 0
instead, to check whether a number is even or odd.
Upvotes: 1