C# Program to check number is even or odd using class


In this program, You will learn how to check number is even or odd using class and object in C#.


if (n % 2 == 0) { 
   //statement
}

Example: How to check number is even or odd using class in C#.

using System;
public class Program
{
	public int check(int n)
	{
		if (n % 2 == 0)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	public static void Main(string[] args)
	{
		int x, res;

		Console.Write("Enter a number:");
		x = Convert.ToInt32(Console.ReadLine());

		Program obj = new Program();
		res = obj.check(x);

		if (res == 1)
		{
			Console.WriteLine("Number is even:" + x);
		}
		else
		{
			Console.WriteLine("Number is odd:" + x);
		}
	}
}

Output:

Enter a number:14
Number is even:14

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.