Header Ads

Header ADS

Program for checking Even or Odd Number

Introduction

Hello friends, In this post, we will learn how to write a simple program in C for checking weather a number is even or odd.
This tutorial is for those people who want to learn programming in C and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.
Here we are calling two standard library functions. Whenever we call the library functions we must write their prototype before making the call.This helps the compiler in checking whether the values being passed and returned are as per the prototype declaration.But since we don’t define the library functions (we merely call them) we may not know the prototypes of library functions. Hence when the library of functions is provided a set of ‘.h’ files is also provided.These files contain the prototypes of library functions. But why multiple files? Because the library functions are divided into different groups and one file is provided for each group. For example, prototypes of all input/output functions are provided in the file ‘stdio.h’, prototypes of all mathematical functions are provided in the file ‘math.h’, etc.
On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. You can even open this file and look at the prototypes.

Program Briefing

First, we call the standard library function 'stdio.h' because prototypes of all input/output functions are provided in the file ‘stdio.h’. Then we call the standard library function 'conio.h' because On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. Then comes the program's main body.We declare an integer variable 'a'. We ask the number which is to be checked and store it in variable 'a'. We divide this number by 2 and if reminder is '0' then we print the number as even else we print it as odd.

Program

//Program for checking Even or Odd Number :

 #include<stdio.h>
 #include<conio.h>

 void main()
 {
   int a;
   clrscr();
   printf("Enter the number\n");
   scanf("%d", &a);
   clrscr();
   printf("Number = %d",a);
 
   if(a % 2 == 0)
   {
     printf("\n\n\nThe Number is Even\n");
   }
 
   else
   printf("\n\n\nThe Number is Odd\n");

   getch();
 }



No comments

Powered by Blogger.