Check whether a number is an Even or Odd program in C

check-number-is-even-or-odd-program-in-c

Description:

Check whether the given number is an Even or Odd Program in C using the if else statement.

The Program will request the user to enter a positive number, Based on the user input number program will display the result, whether the given number is an Even number or an Odd Number.

Expected Output:

Pre-Requisites:

You need to know the basics of decision-making statements like if else statement. Please read the following article.

Program: Even or Odd program in C:

Even or Odd program in C Output:

Program Explanation or Algorithm:

  • The program will start by asking the user for a Positive Number.
  • Once the user enters a Number, we store it in number variable.
  • Then we are checking if the given number is an Even Number of Odd number using the modulus operator
  • The number % 2 == 0 , Means We are modulo dividing the number with the 2. If the result of number % 2 is zero, Then the number is an Even Number ( As it is perfectly divisible by the 2). So we will print the message saying Given Number is Even Number
  • If the number % 2 is not equal to zero, Then it is equivalent to 1, Which means the number is Odd Number, So we will print the Odd Number message on the console.

Even or Odd Program in C with Positive number check:

Let’s re-write the above program by adding an extra check to show the error message if the user enters a Negative number.

We are going to use the goto statement to take control back the user input statement (printf/scanf)

Program:

Program Output:

As you can see from the above output, We are only allowing positive numbers. If the user enters the Negative Number, Then we are displaying the error message ERROR: Negative number provided. Enter Positive Number and took the program control back to the printf statement, so that the user can enter the number again.

Here we used the goto statement to jump from one statement in the program to another statement.

More C Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] Check given number is positive or negative number or Zero […]

  2. […] earlier programs, we discussed the Even or Odd number program using the if else statement. In today’s article, We are going to write a C program to check Even or Odd number using […]

  3. […] C Program to check whether a number is Even or Odd […]

Leave a Reply