Best Cell Phones

Even or Odd

Even or Odd - C Programming Language Example

This programming example will take a number as an input and will determine whether the number is even or odd.

C Programming Code

#include<stdio.h>
#include<conio.h>
main()
{
    int a;
    printf("Input the number \n");
    scanf("%d",&a);
    if(a%2==0)
    {
        printf("The number is even \n");
    }
    else
    {
        printf("The number is odd \n");
    }
    getch();
}

Output

C Programming Example
Even number output

C Programming Example
Odd number output

Explanation

·         #include<stdio.h>
#include<stdio.h>
Ø  This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>.In the other hand  <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.

·         main()
Ø  In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·         int a;
Ø  int is a variable or data type. A variable declared to be of type integer can be used for storing integer numbers. Here we declare “a” variable in int type.
·         printf() function
Ø  C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·         scanf() function
Ø  This function is used to get input from the user of the program. scanf(“%d”,&a”) is used here. So, this program will read in a integer value for “a” variable that the user enters on the keyboard (%d is for integer values. As there is “&a”, the number user enters will be the value of “a” variable).

·         if(a%2==0)
Here if loop is used and the condition a%2==0 is set within ( ) as it is customary to set the condition within the barckets. a%2==0  means that if the integer number is divisible by 2 and the reminder is 2 then it will be an even number.
    {
        printf("The number is even \n");
    }
Is written like this in another second bracket cause after every loop a code within a second bracket is required. And if the conditions meet then the program will show that it is an even number like written in the brackets after printf
If the conditions doesn’t mean then an ELSE loop is also set so that any other form of results is received the program will show that it is an odd number like written in the code.
   else
    {
        printf("The number is odd \n");
    }
Then finally getch(); is written to end the code.




No comments

Powered by Blogger.