CREATE OWN LIBRARY

Even and Odd numbers

Back to Programming

Description

The program is written here to check whether a number is an even number or odd. 

 

# If the number is divisible by 2 then the number is said an even number, otherwise it is an odd number. 

 

# The modulus operator is used here to check whether the number is divisible by 2 or not.

 

For example, 

If the given number is 10, it is divisible by 2, therefore, it is an even number.

If the number is 5, it is not divisible by 2, so, it is an odd number.

 

Algorithm

INPUT: A number

OUTPUT: Whether it is an even number or odd number

PROCESS:

Step 1: [Taking the input]

               Read n [the number]

Step 2: [Checking whether the number is an even number or odd number]

               If n mod 2=0 then 

                              Print “The number is an even number”

               Else

                              Print “The number is an odd number”

               [End of ‘if’]

Step 3: Stop.

Code

TIME COMPLEXITY:

if(n%2==0) ------------------------ O(1)

                              printf("\nEven number");

               else ---------------------------------- O(1)

                              printf("\nOdd Number");

 

The time complexity of this program is O(1) means it requires a constant time to execute.

 

SPACE COMPLEXITY:

The space complexity of this program is O(1) as it requires a constant number of spaces for any given input.