Odd-Even program in C:

An even number is perfectly divisible by 2, whereas an odd number is not perfectly divisible by 2.

We use this logic to check if a number is even or odd.

The below code checks if the integer entered by the user is even or odd:

#include <stdio.h>

int  main()

{

   int  number;

   printf("Enter an integer: ");

   scanf("%d", & amp; number);

   if (number % 2  == 0)

      printf("%d is even.", number);

   else

      printf("%d is odd.", number);

   return  0;

}
  • We have created an integer variable number to store the user input.
  • We are checking if the integer entered by the user is perfectly divisible by 2 or not. If it is, then it is an even number; else, it is an odd number.
  • We use the modulus operator to check for divisibility. If a%b is equal to 0, it implies that a is perfectly divisible by b.

We run the code for different inputs and get the outputs accordingly.

Some inputs and outputs for the code are:

Enter an integer:  4

4 is even.

Enter an integer:  5

5 is odd.

Enter an integer:  -54

-54 is even.

Enter an integer:  12223

12223 is odd.

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019