C Program to check whether number is odd or even | LearntHub

Hello friends, in this post today, we will learn that how we can check that whether number is odd or even in C programming language.
Before starting the program, we need to know what is Odd and Even numbers.
Let's start to know that what is odd and even numbers.
Odd or even numbers

Odd Numbers:-

Odd numbers are a type of numbers, even numbers are called the numbers that are not completely divisible by two.
For example: 3, 5, 7, 9...

Even Numbers:-

Even numbers are a type of numbers like odd numbers.
  1. Operators
  2. Conditions Statements
  3. Loops
Odd numbers are not divisible by two but even numbers are divisible.
For example: 2, 4, 6, 8...
Friends, we have learned that what is odd and even numbers from the definition given above, and now we will know how we can check whether number is odd and even number by a program using if statement.
Let's know how we can check whether number is odd or even by C program.

1. Output:
Enter any number:8
Number is odd.

2. Output:
Enter any number:13
Number is even.

In the above programs, we are need few things to explain which are as follow:
  • In the first line #include<stdio.h> allow the program to the file of  PC, #include is preprocessor directive, it include the information to exicute specific functions from specified header files. The space between # and include is not allow at compile time.
  • In the second line main() is main function. Every program contains a function which called main function. It declear the start of the function. Curly brackets({}) use to start and close functions, it uses to group statements togather in functions, which called a block.
  • In the next line, int a is the variable variables declaration. This use to store integer numeric data. Int is a integer data type, which store integer number like 1,2,3,9,4 etc.
  • Next is printf("......"); Here printf is buil in function in C, used to print(output) keywords on the screan, bracket uses to write a statement between brackets. Double quotes use to inclosed printed text in double quotes. \n used for start next statement to new line. Very importante notes is that every compound statement ends with semicolon (;).
  • Next is scanf("%d", &a); Like printf, scanf is a built in function in c. It uses to read the value of the entered variables and assigns the value to specified variables. %d in the scanf or printf functions tells to the compiler that the value of  the variables should read or print as a integer. The ampersand (&) is an oprator which used to specifies the address of variables.
  • Next is if(a%2==0) This is a particular type of expression statement which called statement. This uses to find the number is odd or even.
  • For example: if we want to chack that 6 is odd or even, we put 6 at the place of a i.e,.       a%2==0
      6%2==0
      Or if we take 5 at the place of 6 then we          find 5%2==1
      Thus 6 is odd and 5 is even number.

  • In the Next step we use if-else statement, if the condition one is true it exicute 1st statement. And else uses for, if condition one is false then, statement 2 will have exicuted.
  • Next and final step is return statement, it used to return a value.

Post a Comment

1 Comments