Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Check whether a number is even or odd


C# Sharp Conditional Statement: Exercise-2 with Solution

Write a C program to check whether a given number is 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 Exercise2  
{  
    public static void Main()  
    {
    int num1, rem1;
    Console.Write("\n\n");
    Console.Write("Check whether a number is even or odd :\n");
    Console.Write("---------------------------------------");
    Console.Write("\n\n");
    Console.Write("Input an integer : ");
    num1= Convert.ToInt32(Console.ReadLine()); 
    rem1 = num1 % 2;
    if (rem1 == 0)
 Console.WriteLine("{0} is an even integer.\n",num1);
    else
 Console.WriteLine("{0} is an odd integer.\n",num1);
}
}

Sample Output:

Check whether a number is even or odd :                                                                       
---------------------------------------
Input an integer : 10                                                                                         
10 is an even integer.

Flowchart:
Flowchart: Check whether a number is even or odd.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program to accept two integers and check whether they are equal or not.
Next: Write a C# Sharp program to check whether a given number is positive or negative.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.