Thursday, April 03, 2014

Odd Even Check – C Program

Problem Question


Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.

Explanation of Problem


The program accepts the user input, an integer, and checks whether it is an odd number or even number.

Code


#include <stdio.h>
/*Odd Even Check*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: CodeBlocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 03-04-2014**/
int main()
{
    int num = 0;
    printf("\n\nEnter a number(integer): ");
    scanf("%d", &num);
    if (num%2)
    {
        printf("\n\nODD");
    }
    else
    {
        printf("\n\nEVEN\n\n");
    }
    system("pause");
    return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

num%2 -> returns 0 if the number is even, else the number is odd. So the statement if (num%2) reflects the condition to be executed if the expression num%2 returns a number other than 0. Thus we print odd in that case. The else condition reflects the case when the expression num%2 returns 0, that means a number is completely divisible by 2 and leaves a remainder 0. Thus the the number should be even in that case. The ‘%’ (modulus operator) finds the remainder on dividing the number on left by the number on right of the ‘%’. Thus, an even number on division with 2 would leave a remainder 0 (the else condition) and an odd number would leave a remainder 1 (the if condition).

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



No comments:

Post a Comment

Need help?