Check whether a number is an 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:
1 2 |
Enter a positive Number : 10 Given number 10 is Even Number |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/* Program: Even or Odd number program in C Author: Sillycodes.com */ #include<stdio.h> int main() { int number; // Ask the user for the input number printf("Enter a positive Number : "); scanf("%d", &number); // We are going to use the `Modulus Operator` if(number % 2 == 0) { // Even Number printf("Given number %d is Even Number\n", number); } else { // Odd Number printf("Given number %d is Odd Number\n", number); } return 0; } |
Even or Odd program in C Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ gcc even-or-odd.c $ ./a.out Enter a positive Number : 10 Given number 10 is Even Number $ ./a.out Enter a positive Number : 7 Given number 7 is Odd Number $ ./a.out Enter a positive Number : 440 Given number 440 is Even Number $ ./a.out Enter a positive Number : 89 Given number 89 is Odd Number $ ./a.out Enter a positive Number : 3287623 Given number 3287623 is Odd Number $ ./a.out Enter a positive Number : 221220 Given number 221220 is Even Number $ |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* Program: Even or Odd number program in C Author: Sillycodes.com */ #include<stdio.h> int main() { int number; USER_INPUT: // Ask the user for the input number printf("Enter a positive Number : "); scanf("%d", &number); // Only allow the positive numbers if (number < 0) { // 'number' is Negative // Display error message and take back to user input printf("ERROR: Negative number provided. Enter Positive Number\n"); goto USER_INPUT; } // We are going to use the `Modulus Operator` if(number % 2 == 0) { // Even Number printf("Given number %d is Even Number\n", number); } else { // Odd Number printf("Given number %d is Odd Number\n", number); } return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ gcc even-or-odd.c $ ./a.out Enter a positive Number : 46 Given number 46 is Even Number $ ./a.out Enter a positive Number : 100 Given number 100 is Even Number $ ./a.out Enter a positive Number : -10 ERROR: Negative number provided. Enter Positive Number Enter a positive Number : -1 ERROR: Negative number provided. Enter Positive Number Enter a positive Number : -567 ERROR: Negative number provided. Enter Positive Number Enter a positive Number : 998 Given number 998 is Even Number $ |
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.
3 Responses
[…] Check given number is positive or negative number or Zero […]
[…] 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 […]
[…] C Program to check whether a number is Even or Odd […]