w3resource

C Exercises: Check if a given integer is even or odd using an inline function


9. Even/Odd Check Inline Variants

Write a C program to check if a given integer is even or odd using an inline function.

Sample Solution:

C Code:

#include <stdio.h>

inline int isEven(int n) {
  return n % 2 == 0;
}
int main() {
  int num;
  printf("Input an integer: ");
  scanf("%d", & num);
  if (isEven(num)) {
    printf("%d is even.\n", num);
  } else {
    printf("%d is odd.\n", num);
  }
  return 0;
}

Sample Output:

Input an integer: 3
3 is odd.
Input an integer: 46
46 is even.

Explanation:

In the above program we first define an inline function isEven() that takes an integer as an argument and returns a boolean indicating whether the integer is even or not. The main function then prompts the user to input an integer, passes it to the isEven() function, and prints a message indicating whether the integer is even or odd.

Flowchart:

Flowchart: Check if a given integer is prime using an inline function.

For more Practice: Solve these Related Problems:

  • Write a C program to check if a number is even or odd using bitwise operators in an inline function.
  • Write a C program to return "Even" or "Odd" as a string from an inline function based on the input integer.
  • Write a C program to determine the parity of each element in an array using an inline function and count evens and odds.
  • Write a C program to check the parity of a series of numbers and print a summary using an inline function.

Go to:


PREV : Prime Check Inline Variants.
NEXT : Reverse String Inline Variants.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.