w3resource

C#: Program to test even or odd


C# Sharp Basic Data Types: Exercise-10 with Solution

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

Even Numbers between 1 to 100:

Even Numbers between 1 to 100

Calculating a Odd Numbers:

Calculating a Odd Numbers

Odd Numbers between 1 to 100:

Odd Numbers between 1 to 100

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.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/data-types/csharp-data-type-exercise-10.php