Javatpoint Logo
Javatpoint Logo

Even odd programs in C

Introduction to Even-Odd Program in C

The even-odd program is a simple C program that assists in identifying whether a given integer is even or odd. In everyday language, we typically organize integers as even or odd based on their divisibility by 2.

Even numbers can be divided by two without leaving a leftover. Even numbers include two, four, six, and eight. However, odd numbers cannot be divided by 2 fairly and leave a remainder. Odd numbers include 1, 3, 5, and 7.

The even-odd program in C allows us to enter a number and determine whether it is even or odd. This choice is made using a simple algorithm.

Approach and Logic

The approach and logic for the even-odd program in C involve checking whether a given number is divisible evenly by 2. Here's the general approach and logic for the program:

  • Start by prompting the user to enter a number.
  • Please read the user input and store it in an integer variable, such as a number.
  • Use an if statement to check whether the number is divisible evenly by 2. It can be done by checking if the remainder of the number divided by 2 is equal to 0 (number % 2 == 0).
  • If the condition is true (i.e., the remainder is 0), the number is even.
  • Display a message indicating that the number is even.
  • The number is odd if the condition is false (i.e., the remainder is not 0).
  • Display a message indicating that the number is odd.
  • End the program.

Here's an example implementation of the approach and logic described above:

Code

Output:

Enter a number: 5
5 is an odd number.

Explanation:

In this implementation, the program prompts the user to enter a number, reads the input, checks whether the number is even or odd using the % modulus operator, and displays the corresponding message based on the check result.

Variable Declaration

In the even-odd program in C, you would typically declare a single variable to store the user input number. Here's an example of variable Declaration in the even-odd program:

Code

Explanation:

In the above code, the int number; statement declares an integer variable named number. This variable stores the user input number that will be checked for evenness or oddness.

The int keyword indicates that the number is an integer variable. You can modify the name of the variable as per your preference. It's important to note that variable names in C should be meaningful and descriptive.

By declaring the number variable, you allocate memory to store an integer value entered by the user. This allows you to store and manipulate the user input throughout the program.

Even odd programs in C

Here's an example of a program in C that determines whether a given number is even or odd:

Code

Output:

Enter a number: 17
17 is an odd number.

Explanation:

  • The program begins by declaring an integer variable number to store the input value.
  • It prompts the user to enter a number by using printf to display the message "Enter a number: ".
  • The scanf function reads an integer value from the user and saves it in the number variable.
  • The program then uses an if statement to determine whether the number is evenly divided by two (i.e., the remainder of number % 2 is 0).
  • If the condition is met, the message "%d is an even number."n is printed using printf, where %d is a placeholder for the number variable.
  • If the condition is false, the else block is executed, and the message "%d is an odd number."n is printed using printf.
  • Finally, the return 0 statement signals that the program was successfully executed.

When you run this program, it will prompt you to enter a number. After entering the number, it will determine whether it is even/odd and display the appropriate message.

In the program, the user is prompted to enter a number using printf. The user's input is read and stored in the number variable using scanf.

Then, an if statement determines whether the integer is evenly divisible by 2. If the criterion (number% 2 == 0) is met, the program publishes the message with an even number. If the condition is false, the program performs the else block and publishes the message that the number is odd.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA