Odd even program in c language for beginners

Beginners code hub
By -
0
#include <stdio.h>

int main() {
    int num;

    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }

    return 0;
}
    

 Steps in the Program


1. Include Necessary Libraries:

The program begins by including the standard input/output library (stdio.h). This library provides built-in functions to handle input and output, such as printing text to the screen and reading user input.

2. Declare the Main Function:

Every C program starts executing from the main function. It acts as the entry point.

3. Variable Declaration:

The program declares an integer variable to store the number entered by the user. This variable is used to perform the even or odd check.

4. Prompt the User for Input:

A message is displayed on the screen asking the user to input an integer. This is done using the printf function.

5. Read the User's Input:

The program waits for the user to enter a number. The input is captured and stored in the previously declared variable using the scanf function.

6. Perform the Odd/Even Check:

The program checks if the number is divisible by 2. This is done using the modulus operator (%), which calculates the remainder when one number is divided by another.

If the remainder is 0, the number is even.

If the remainder is not 0, the number is odd.


7. Display the Result:

Based on the result of the modulus operation:

If the number is even, a message is printed saying the number is "even."

If the number is odd, a message is printed saying the number is "odd."


8. End the Program:

The program concludes by returning a value (usually 0) to indicate successful execution.


How It Works

1. Output for Odd:
Odd even program in c language for beginners

2. Output for Odd:
Odd even program in c language for beginners




Tags:

Post a Comment

0Comments

Post a Comment (0)