Menu
×
Tutorials Ms Word Tutorial Ms Excel Tutorial Ms PowerPoint Tutorial C Language Tutorial C++ Tutorial C Sharp Tutorial Visual Basic Tutorial HTML Tutorial CSS Tutorial JavaScript Tutorial WordPress Tutorial
     ❯   

ADVERTISEMENT

Weather Given Number is Odd or Even

This C program determines whether a given integer is even or odd.

#include<stdio.h>
main()
{
  int i;
  printf("Enter a Number\n");
  scanf("%d",&i);
  if(i%2==0)
  {
    printf("The number %d is Even",i);
  }
  else
  {
    printf("The number %d is Odd",i);
  }
  printf("\n");
}
C Programming Examples

OUTPUT
Enter a Number
7
The number 7 is Odd
C Programming Examples

Explanation:

Here’s a step-by-step explanation of the code:

Header File:

  • The program includes the standard C library header <stdio.h>. This header is required for input and output operations.

Main Function:

  • The main function is the entry point of the program, where the execution starts.
  • Variable Declaration:
  • The program declares an integer variable ‘i’ to store the number entered by the user.

User Input:

  • The program prompts the user with the message “Enter a Number” to input an integer. It uses printf to display the message and scanf to read the input from the user, storing it in the variable ‘i’.

Checking Even or Odd:

  • The program then enters a conditional statement, which is an if-else statement. It checks if ‘i’ is even or odd.
  • The condition within the if statement is i % 2 == 0. This condition checks if the remainder (modulus) of ‘i’ divided by 2 is equal to 0. If this condition is true, it means ‘i’ is an even number.
  • If the condition is true (i.e., ‘i’ is even), the program uses printf to display the message “The number [i] is Even,” where ‘[i]’ is replaced with the actual value of ‘i’.
  • If the condition is false (i.e., ‘i’ is odd), the program executes the else block. It uses printf to display the message “The number [i] is Odd,” replacing ‘[i]’ with the value of ‘i’.

Newline Character:

  • After determining whether ‘i’ is even or odd and displaying the corresponding message, the program uses printf with “\n” to print a newline character. This adds a line break, moving the cursor to the beginning of the next line.

End of Program:

  • The program reaches the end of the main function and, consequently, the end of the program’s execution.

This code takes an integer input from the user, checks whether it’s even or odd, and prints a message indicating whether the number is even or odd. It also adds a newline character for formatting purposes.

Spread the love
Scroll to Top
Scroll to Top