C Language Program to Determine if a Number is Even or Odd

Determine if a number is even or odd

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>  // system function required
int main(){    int a;
    while (1)  // infinite loop    {        printf("Please enter an integer:\n");        scanf("%d", &a);
        if (a % 2 == 0)        {            printf("%d is even\n", a);        }        else        {            printf("%d is odd\n", a);        }
        printf("Press any key to continue...\n");        getchar(); // consume newline        getchar(); // wait for user key press
        system("cls"); // clear screen;    }
    return 0;}

The C language program determines the parity of a number,translated into natural language English:

#define _CRT_SECURE_NO_WARNINGS — DisablesVisual Studiospecific security warnings (e.g.,scanfmay be unsafe). Other compilers generally do not require this.

#include <stdio.h> ——- Standard input/output library, used forprintf,scanfand other functions.

#include <stdlib.h> // system function required —- Standard library header file, includes system functions likesystem()to execute system commands (e.g., clear screen).

int main() —— Program entry point, marks the start of the main function

{

int a; —— Defines an integer variablea, used to store the user input number

while (1) ——– Infinite loop, the program will continue running until manually terminated

{

printf(“Please enter an integer:\n”); —- Displays a prompt to the user to enter an integer

scanf(“%d”, &a); ——– Reads the user input integer and stores it in variablea

if (a % 2 == 0) ——- Ifamodulo 2 equals 0

{

printf(“%d is even\n”, a); —- Outputsais even message

}

else —– Ifais not divisible by2, remainder is not 0, it is odd

{

printf(“%d is odd\n”, a); —— Outputsais odd message

}

printf(“Press any key to continue…\n”); ——- Prompts the user to press any key to continue the program

getchar(); // Consume newline ———- Reads and discards the newline left by the previous input to avoid interference with the next input

getchar(); // Wait for user key press ——– Waits for the user to press any key to continue program execution

system(“cls”); // Clear screen; ——– Calls the system command to clear the screen (only applicable toWindows)

}

return 0; ———– Program ends normally and returns0to the operating system

}

Program output:

C Language Program to Determine if a Number is Even or OddC Language Program to Determine if a Number is Even or Odd

Leave a Comment