Posted on by Kalkicode
Code Number

Check whether a given number is even or odd

In this article, we will discuss a simple C program to determine whether a given integer is even or odd. We will first understand the problem statement and then proceed to explain the code to check for even or odd numbers. We will provide a suitable example to demonstrate the working of the program and analyze its time complexity.

Problem Statement

The task is to write a C program that takes an integer as input and checks whether it is an even or an odd number. An even number is any integer that can be exactly divided by 2, resulting in a remainder of 0, while an odd number will have a remainder of 1 when divided by 2.

Example

Let's consider the number 12. Its binary representation is 1100. Since the last bit (least significant bit) is 0, it is an even number. On the other hand, the number 7 has a binary representation of 111, with the last bit being 1, making it an odd number.

Pseudocode

To check whether a given number is even or odd, we can use the modulo operator (%) to find the remainder when the number is divided by 2. If the remainder is 0, the number is even, and if the remainder is 1, the number is odd.

checkEvenOdd(num)
    if num % 2 == 0
        print "Is Even Number"
    else
        print "Is Odd Number"

Algorithm

  1. Start the program and define the function checkEvenOdd(num).
  2. Display the given number using the printf() function.
  3. Check whether the number is even or odd using the condition num % 2 == 0.
  4. If the condition is true, print "Is Even Number"; otherwise, print "Is Odd Number".
  5. Return to the main function.
  6. Test the checkEvenOdd function with various test cases.

Explanation

The provided C program begins with a function checkEvenOdd(num) that takes an integer num as input. The function prints the given number and then checks if the number is even or odd using the condition num % 2 == 0. The modulo operator % calculates the remainder when num is divided by 2. If the remainder is 0, the number is even, and "Is Even Number" is printed; otherwise, "Is Odd Number" is printed.

Test Cases:

  1. Test Case: checkEvenOdd(12) Output: Number: 12, Is Even Number

  2. Test Case: checkEvenOdd(34) Output: Number: 34, Is Even Number

  3. Test Case: checkEvenOdd(-12) Output: Number: -12, Is Even Number

  4. Test Case: checkEvenOdd(7) Output: Number: 7, Is Odd Number

  5. Test Case: checkEvenOdd(21) Output: Number: 21, Is Odd Number

  6. Test Case: checkEvenOdd(-11) Output: Number: -11, Is Odd Number

Code Solution

Here given code implementation process.

// C Program 
// Check whether a given number is even or odd
#include <stdio.h>
 // Determine whether given number is Even or Odd
void checkEvenOdd(int num)
{
	// Display given number
	printf("\n Number : %d", num);
	if (num % 2 == 0)
	{
		// When last bit is not set
		printf("\n Is Even Number \n");
	}
	else
	{
		printf("\n Is Odd Number\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Cases
	// 12 (1100)
	checkEvenOdd(12);
	// 34 (100010)
	checkEvenOdd(34);
	// -12 (∞11110100)
	checkEvenOdd(-12);
	// 7 (111)
	checkEvenOdd(7);
	// 21 (00010101)
	checkEvenOdd(21);
	// -11 (11110101)
	checkEvenOdd(-11);
	return 0;
}

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
/*
  Java Program 
  Check whether a given number is even or odd
*/
public class Comparison
{
    // Determine whether given number is Even or Odd
    public void checkEvenOdd(int num)
    {
        // Display given number
        System.out.print("\n Number : " + num );
        if (num % 2 == 0)
        {
            // When last bit is not set
            System.out.print("\n Is Even Number \n");
        }
        else
        {
            System.out.print("\n Is Odd Number\n");
        }
    }
    public static void main(String[] args)
    {
        Comparison task = new Comparison();
        // Test Cases
        // 12 (1100)
        task.checkEvenOdd(12);
        // 34 (100010)
        task.checkEvenOdd(34);
        // -12 (∞11110100)
        task.checkEvenOdd(-12);
        // 7 (111)
        task.checkEvenOdd(7);
        // 21 (00010101)
        task.checkEvenOdd(21);
        // -11 (11110101)
        task.checkEvenOdd(-11);
    }
}

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program 
  Check whether a given number is even or odd
*/
class Comparison
{
	public:
		// Determine whether given number is Even or Odd
		void checkEvenOdd(int num)
		{
			// Display given number
			cout << "\n Number : " << num;
			if (num % 2 == 0)
			{
				// When last bit is not set
				cout << "\n Is Even Number \n";
			}
			else
			{
				cout << "\n Is Odd Number\n";
			}
		}
};
int main()
{
	Comparison task = Comparison();
	// Test Cases
	// 12 (1100)
	task.checkEvenOdd(12);
	// 34 (100010)
	task.checkEvenOdd(34);
	// -12 (∞11110100)
	task.checkEvenOdd(-12);
	// 7 (111)
	task.checkEvenOdd(7);
	// 21 (00010101)
	task.checkEvenOdd(21);
	// -11 (11110101)
	task.checkEvenOdd(-11);
	return 0;
}

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
// Include namespace system
using System;
/*
  C# Program 
  Check whether a given number is even or odd
*/
public class Comparison
{
	// Determine whether given number is Even or Odd
	public void checkEvenOdd(int num)
	{
		// Display given number
		Console.Write("\n Number : " + num);
		if (num % 2 == 0)
		{
			// When last bit is not set
			Console.Write("\n Is Even Number \n");
		}
		else
		{
			Console.Write("\n Is Odd Number\n");
		}
	}
	public static void Main(String[] args)
	{
		Comparison task = new Comparison();
		// Test Cases
		// 12 (1100)
		task.checkEvenOdd(12);
		// 34 (100010)
		task.checkEvenOdd(34);
		// -12 (∞11110100)
		task.checkEvenOdd(-12);
		// 7 (111)
		task.checkEvenOdd(7);
		// 21 (00010101)
		task.checkEvenOdd(21);
		// -11 (11110101)
		task.checkEvenOdd(-11);
	}
}

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
<?php
/*
  Php Program 
  Check whether a given number is even or odd
*/
class Comparison
{
	// Determine whether given number is Even or Odd
	public	function checkEvenOdd($num)
	{
		// Display given number
		echo "\n Number : ". $num;
		if ($num % 2 == 0)
		{
			// When last bit is not set
			echo "\n Is Even Number \n";
		}
		else
		{
			echo "\n Is Odd Number\n";
		}
	}
}

function main()
{
	$task = new Comparison();
	// Test Cases
	// 12 (1100)
	$task->checkEvenOdd(12);
	// 34 (100010)
	$task->checkEvenOdd(34);
	// -12 (∞11110100)
	$task->checkEvenOdd(-12);
	// 7 (111)
	$task->checkEvenOdd(7);
	// 21 (00010101)
	$task->checkEvenOdd(21);
	// -11 (11110101)
	$task->checkEvenOdd(-11);
}
main();

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
/*
  Node Js Program 
  Check whether a given number is even or odd
*/
class Comparison
{
	// Determine whether given number is Even or Odd
	checkEvenOdd(num)
	{
		// Display given number
		process.stdout.write("\n Number : " + num);
		if (num % 2 == 0)
		{
			// When last bit is not set
			process.stdout.write("\n Is Even Number \n");
		}
		else
		{
			process.stdout.write("\n Is Odd Number\n");
		}
	}
}

function main()
{
	var task = new Comparison();
	// Test Cases
	// 12 (1100)
	task.checkEvenOdd(12);
	// 34 (100010)
	task.checkEvenOdd(34);
	// -12 (∞11110100)
	task.checkEvenOdd(-12);
	// 7 (111)
	task.checkEvenOdd(7);
	// 21 (00010101)
	task.checkEvenOdd(21);
	// -11 (11110101)
	task.checkEvenOdd(-11);
}
main();

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
#   Python 3 Program 
#   Check whether a given number is even or odd

class Comparison :
	#  Determine whether given number is Even or Odd
	def checkEvenOdd(self, num) :
		#  Display given number
		print("\n Number : ", num, end = "")
		if (num % 2 == 0) :
			#  When last bit is not set
			print("\n Is Even Number ")
		else :
			print("\n Is Odd Number")
		
	

def main() :
	task = Comparison()
	#  Test Cases
	#  12 (1100)
	task.checkEvenOdd(12)
	#  34 (100010)
	task.checkEvenOdd(34)
	#  -12 (∞11110100)
	task.checkEvenOdd(-12)
	#  7 (111)
	task.checkEvenOdd(7)
	#  21 (00010101)
	task.checkEvenOdd(21)
	#  -11 (11110101)
	task.checkEvenOdd(-11)

if __name__ == "__main__": main()

Output

 Number :  12
 Is Even Number

 Number :  34
 Is Even Number

 Number :  -12
 Is Even Number

 Number :  7
 Is Odd Number

 Number :  21
 Is Odd Number

 Number :  -11
 Is Odd Number
#   Ruby Program 
#   Check whether a given number is even or odd

class Comparison 
	#  Determine whether given number is Even or Odd
	def checkEvenOdd(num) 
		#  Display given number
		print("\n Number : ", num)
		if (num % 2 == 0) 
			#  When last bit is not set
			print("\n Is Even Number \n")
		else 
			print("\n Is Odd Number\n")
		end

	end

end

def main() 
	task = Comparison.new()
	#  Test Cases
	#  12 (1100)
	task.checkEvenOdd(12)
	#  34 (100010)
	task.checkEvenOdd(34)
	#  -12 (∞11110100)
	task.checkEvenOdd(-12)
	#  7 (111)
	task.checkEvenOdd(7)
	#  21 (00010101)
	task.checkEvenOdd(21)
	#  -11 (11110101)
	task.checkEvenOdd(-11)
end

main()

Output

 Number : 12
 Is Even Number 

 Number : 34
 Is Even Number 

 Number : -12
 Is Even Number 

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
/*
  Scala Program 
  Check whether a given number is even or odd
*/
class Comparison
{
	// Determine whether given number is Even or Odd
	def checkEvenOdd(num: Int): Unit = {
		// Display given number
		print("\n Number : " + num);
		if (num % 2 == 0)
		{
			// When last bit is not set
			print("\n Is Even Number \n");
		}
		else
		{
			print("\n Is Odd Number\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Comparison = new Comparison();
		// Test Cases
		// 12 (1100)
		task.checkEvenOdd(12);
		// 34 (100010)
		task.checkEvenOdd(34);
		// -12 (∞11110100)
		task.checkEvenOdd(-12);
		// 7 (111)
		task.checkEvenOdd(7);
		// 21 (00010101)
		task.checkEvenOdd(21);
		// -11 (11110101)
		task.checkEvenOdd(-11);
	}
}

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
/*
  Swift 4 Program 
  Check whether a given number is even or odd
*/
class Comparison
{
	// Determine whether given number is Even or Odd
	func checkEvenOdd(_ num: Int)
	{
		// Display given number
		print("\n Number : ", num, terminator: "");
		if (num % 2 == 0)
		{
			// When last bit is not set
			print("\n Is Even Number ");
		}
		else
		{
			print("\n Is Odd Number");
		}
	}
}
func main()
{
	let task: Comparison = Comparison();
	// Test Cases
	// 12 (1100)
	task.checkEvenOdd(12);
	// 34 (100010)
	task.checkEvenOdd(34);
	// -12 (∞11110100)
	task.checkEvenOdd(-12);
	// 7 (111)
	task.checkEvenOdd(7);
	// 21 (00010101)
	task.checkEvenOdd(21);
	// -11 (11110101)
	task.checkEvenOdd(-11);
}
main();

Output

 Number :  12
 Is Even Number

 Number :  34
 Is Even Number

 Number :  -12
 Is Even Number

 Number :  7
 Is Odd Number

 Number :  21
 Is Odd Number

 Number :  -11
 Is Odd Number
/*
  Kotlin Program 
  Check whether a given number is even or odd
*/
class Comparison
{
	// Determine whether given number is Even or Odd
	fun checkEvenOdd(num: Int): Unit
	{
		// Display given number
		print("\n Number : " + num);
		if (num % 2 == 0)
		{
			// When last bit is not set
			print("\n Is Even Number \n");
		}
		else
		{
			print("\n Is Odd Number\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Comparison = Comparison();
	// Test Cases
	// 12 (1100)
	task.checkEvenOdd(12);
	// 34 (100010)
	task.checkEvenOdd(34);
	// -12 (∞11110100)
	task.checkEvenOdd(-12);
	// 7 (111)
	task.checkEvenOdd(7);
	// 21 (00010101)
	task.checkEvenOdd(21);
	// -11 (11110101)
	task.checkEvenOdd(-11);
}

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number
// Rust Program 
// Check whether a given number is even or odd
fn main()
{
	// Test Cases
	// 12 (1100)
	check_even_odd(12);
	// 34 (100010)
	check_even_odd(34);
	// -12 (∞11110100)
	check_even_odd(-12);
	// 7 (111)
	check_even_odd(7);
	// 21 (00010101)
	check_even_odd(21);
	// -11 (11110101)
	check_even_odd(-11);
}
fn check_even_odd(num: i32)
{
	// Display given number
	print!("\n Number : {}", num);
	if num % 2 == 0
	{
		// When last bit is not set
		print!("\n Is Even Number \n");
	}
	else
	{
		print!("\n Is Odd Number\n");
	}
}

Output

 Number : 12
 Is Even Number

 Number : 34
 Is Even Number

 Number : -12
 Is Even Number

 Number : 7
 Is Odd Number

 Number : 21
 Is Odd Number

 Number : -11
 Is Odd Number

Resultant Output Explanation

The program provides the expected output for each test case. The numbers 12, 34, and -12 are even, and the program correctly identifies them as even numbers. Similarly, the numbers 7, 21, and -11 are odd, and the program identifies them correctly as odd numbers.

Time Complexity

The time complexity of this program is constant, O(1), as it performs a fixed number of operations regardless of the input value. The program involves basic arithmetic operations and a conditional check, which takes a constant amount of time to execute. Thus, the time complexity remains constant, making the program efficient for any input size.

Finally

: In this article, we discussed a C program to check whether a given number is even or odd. We provided an explanation of the problem statement, pseudocode, algorithm, and the resultant output for various test cases. The program has a time complexity of O(1), ensuring efficient execution regardless of the input value. Understanding the concept of even and odd numbers and implementing the code to check them is essential for any beginner learning programming in C or similar languages.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment