Reputation: 23
I have a problem with if
and if else
.
I wrote a snippet of code that asks for a person's personal signum and then takes the 9th element out.
But I simply can not find a way where by using if-else
to check whether it is an even number or not, in order to determine if the person is a male or female.
This how my code looks so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace övning_14
{
class ålder
{
static void Main(string[] args)
{
string personNummer;
string indata;
Console.Write("vad är ditt person signum ? "); // simply asks for the num
indata = Console.ReadLine();
personNummer = indata;
kön = int.Parse(personNummer.Substring(9, 1));
}
}
}
Upvotes: 1
Views: 224
Reputation: 21999
Just for fun, this will also works
if((val / 2).ToString(NumberFormatInfo.InvariantInfo).Contains("."))
{
// odd
}
else
{
// even
}
Upvotes: 0
Reputation: 1190
The Following code
public string evenodd(int value)
{
if(value % 2 == 0)
{
return "Even";
}
else
{
return "Odd";
}
}
Upvotes: 0
Reputation: 236
try something along the lines of
if(kön % 2 == 0)
{
//Even number
}
Upvotes: 0
Reputation: 528
You have to check the the kön variable.
public static bool IsEven(int value)
{
return value % 2 == 0;
}
Now you can call
if (IsEven(kön) == true)
{
// even number
}
else
{
// Not even
}
For a description of the % operator in C# you can check MSDN: % Operator
Upvotes: 2