C#: Program to test even or odd
Write a C# Sharp program that takes two numbers as input and returns true or false when both numbers are even or odd.
Calculating a Even Numbers:
![Calculating a Even Numbers](https://www.w3resource.com/w3r_images/check-even-number.png)
Even Numbers between 1 to 100:
![Even Numbers between 1 to 100](https://www.w3resource.com/w3r_images/even-numbers-1-100.png)
Calculating a Odd Numbers:
![Calculating a Odd Numbers](https://www.w3resource.com/w3r_images/check-odd-number.png)
Odd Numbers between 1 to 100:
![Odd Numbers between 1 to 100](https://www.w3resource.com/w3r_images/odd-numbers-1-100.png)
Sample Solution:-
C# Sharp Code:
using System;
public class Exercise10
{
public static void Main()
{
int n1, n2;
bool bothEven;
Console.Write("Input First number: ");
n1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input Second number: ");
n2 = Convert.ToInt32(Console.ReadLine());
//bothEven = ((n1%2!=0) || (n1%2!=0))? false:true;
bothEven = ((n1 % 2 == 0)
&& (n2 % 2 ==0))? true : false;
Console.WriteLine( bothEven ?
"there're numbers bothEven" :
"there's a number odd");
}
}
Sample Output:
Input First number: 4 Input Second number: 6 there're numbers bothEven
Flowchart:
![Flowchart: Program to test even or odd.](https://www.w3resource.com/w3r_images/csharp-datatype-exercise-flowchart-10.png)
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C# Sharp program that takes a character as input and check the input (lowercase) is a vowel, a digit, or any other symbol.
Next: Write a C# Sharp program that takes a decimal number as input and displays its equivalent in binary form.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics