Finding whether a number(integer) is even or odd is quite simple. The logic behind the given program is that , even numbers(like 6) when divided by 2 give 0 as the remainder and odd numbers(like 5) give 1 as the remainder. Here we make use the modulus operator(%) which returns the remainder. For e.g , 6%2 , give the divides the 6 by 2 and returns the remainder(which is 0), hence we get the output as 0. Consider the following program for more information.
// Program to find whether a number is even or odd ( for GCC and similar compilers)
#include <iostream>
int main()
{
int x;
std::cout<<" Enter the number to check : ";
std::cin>>x; // takes input from the user
if ( x%2 == 0 )
std::cout<<" The number entered is EVEN ";
else
std::cout<<" The number entered is ODD ";
return 0;
} // end of main
Note : If the console window shuts down immedeaitly, then add std::cin.get(); before return 0; to fix the problem. :)
// Program to find whether a number is even or odd (for turbo c++ and similar compilers )
{
int x;
cout<<" Enter the number to check : ";
cin>>x; // takes input from the user
if ( x%2 == 0 )
cout<<" The number entered is EVEN ";
else
cout<<" The number entered is ODD ";
return 0;
} // end of main
Note : If the console window shuts down immedeatily, then then add getch(); before return 0; to fix the problem :). The getch() function
is located in the header file <conio.h>
------ Output ------
// Program to find whether a number is even or odd ( for GCC and similar compilers)
#include <iostream>
int main()
{
int x;
std::cout<<" Enter the number to check : ";
std::cin>>x; // takes input from the user
if ( x%2 == 0 )
std::cout<<" The number entered is EVEN ";
else
std::cout<<" The number entered is ODD ";
return 0;
} // end of main
Note : If the console window shuts down immedeaitly, then add std::cin.get(); before return 0; to fix the problem. :)
// Program to find whether a number is even or odd (for turbo c++ and similar compilers )
#include<iostream.h>
using namespace std;
int main()using namespace std;
{
int x;
cout<<" Enter the number to check : ";
cin>>x; // takes input from the user
if ( x%2 == 0 )
cout<<" The number entered is EVEN ";
else
cout<<" The number entered is ODD ";
return 0;
} // end of main
Note : If the console window shuts down immedeatily, then then add getch(); before return 0; to fix the problem :). The getch() function
is located in the header file <conio.h>
------ Output ------
its a real help to the bigginers in programing
ReplyDeleteindeed, its for anyone who has difficulty in searching programs on the net...
ReplyDeletethanks . it helps me a lot
ReplyDeleteShe was asked to leave her family and job immediately, and say nothing to her husband and employer.
ReplyDelete