0% found this document useful (0 votes)
53 views6 pages

C++ Program To Check If Given Integer Is Even or Odd

The document describes a C++ program that checks whether a given integer is even or odd. It does this by using the modulo (%) operator to check if the number is divisible by 2 with no remainder (even) or has a remainder (odd). It then prints out whether the number entered is even or odd. The full code for the program is included along with sample runs showing it correctly identifying numbers as even or odd.

Uploaded by

workineh amare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

C++ Program To Check If Given Integer Is Even or Odd

The document describes a C++ program that checks whether a given integer is even or odd. It does this by using the modulo (%) operator to check if the number is divisible by 2 with no remainder (even) or has a remainder (odd). It then prints out whether the number entered is even or odd. The full code for the program is included along with sample runs showing it correctly identifying numbers as even or odd.

Uploaded by

workineh amare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ Program to Check if a given Integer is Even or Odd

This C+ +Program checks if a given integer is odd or even. Here if a given number is divisible by 2 with the
remainder 0 then the number is even number. If the number is not divisible by 2 then that number will be odd
number.
Here is source code of the C++ program which checks a given integer is odd or even. The C++ program is
successfully compiled and run on a Linux system. The program output is also shown below.

1. /*
2.  * C++ program to check if given integer is even or odd
3.  */
4. #include<iostream>
5. using namespace std;
6.  
7. int main()
8. {
9. int number, remainder;
10.  
11. cout << "Enter the number : ";
12. cin >> number;
13. remainder = number % 2;
14. if (remainder == 0)
15. cout << number << " is an even integer " << endl;
16. else
17. cout << number << " is an odd integer " << endl;
18.  
19. return 0;
20. }
$ g++ main.cpp
$ ./a.out
Enter the number : 3
3 is an odd integer
 
$ ./a.out
Enter the number : 10
10 is an even integer

////////

An integer number which is evenly divisible by 2 is called even number. When I say evenly divisible,
it means that if we divide the even number by 2, it yields no remainder. For example, 2, 4, 6, 8 …are
even numbers.
An integer number which yields a remainder when divided by 2 is known as odd number. For
example, 3, 5, 7, 9 are odd numbers.

Lets write a program to check even odd numbers


Example: Program to check whether the entered number is
even or odd
To understand this program, you should have the knowledge of if-else and user-defined functions in
C++.

#include <iostream>
using namespace std;
bool checkEvenOdd(int num);

int main(){
int num;
bool isEven;
cout<<"Enter any number: ";
//Storing the entered value in variable num
cin>>num;
//Calling the function that checks even odd
isEven = checkEvenOdd(num); 
  if(isEven)   
cout<<num<<" is an even number"; 
  else   
cout<<num<<" is an odd number";

return 0;
}
/* This function checks whether the passed number is even
 * or odd. If the number is even then this function returns
 * true else it returns false.
 */
bool checkEvenOdd(int num){
bool b;
/* If number is perfectly divisible by 2 then it is
* an even number else it is an odd number
*
*/
if (num % 2 == 0)
b=true;
else
b=false;

return b;
}
Output:

Enter any number: 101


101 is an odd number
//////

1. #include<iostream> 
2. using namespace std; 
3. int main() 
4. { float a,b; 
5. cout<<"Enter two numbers:\n"; 
6. cin>>a>>b; 
7. cout<<endl; 
8. if(a>b) 
9. cout<<a<<" is the greatest number"; 
10. else if(a<b) 
11. cout<<b<<" is the greatest number"; 
12. else 
13. cout<<"Both are equal"; 
14. return(0);} 

#include <iostream>
02 #include <vector>
03 #include <algorithm>
04  
05  
06 using namespace std;
07 int main()

08 {

09     int buf, amount;
10     vector<int> v(0);
11  
12     cout<<"Enter amount of Numbers : ";
13     cin>>amount;
14      

15     for (int i=0; i<amount; i++)

16     {
17         cout<<"Enter Number "<<i+1<<" : ";
18         cin>>buf;
19         v.push_back(buf);
20     }

21   
22    sort( v.begin(), v.end() );
23   

24    cout << "After sorting: ";


25    for( unsigned int i = 0; i < v.size(); i++ )

26      cout << v[i] << " ";

27  
28    cin.ignore();
29    cin.get();
30    return 0;
# 4.

#include <iostream>
using namespace std;

int main()
{
int i = 1, sum=0;
while (i <= 100) {
sum = sum+i;
i++;
}
cout << "\n The sum of numbers from 1 to 100 is: "<<sum << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int sum=0;

for(int i=1; i<=100; i++)


{
// adding 1 to 100 numbers
sum=sum+i;
}
cout << "\n The sum of numbers from 1 to 100 is: "<<sum << endl;
return 0;
}

Output:

The sum of numbers from 1 to 100 is: 5050

#5.
#include <iostream>

using namespace std;

int main()

int i, n, sum = 0;

// Take input from user.

cout << "Print sum of even numbers till : ";

cin >> n; for(i = 0; i <= n; i++)

// Check for even or not.

if((i % 2) == 0)

{ sum += i; } }

cout << endl << "Sum of even numbers from 0 to " << n << " is : " <<
sum;

return 0;

#include <iostream>

using namespace std;

int main()
{
int a, b ,c,sum;
float average;
cin>>a>>b>>c;
sum = a+b+c;
cout<<"The sum of 3 numbers is:"<<sum<<endl;
average =sum/3;
cout<<"The average of 3 numbers is:"<<sum<<endl;
return 0;
}
# 7

#include
using namespace std;
int main()
{
double m[3]{}, *p = m, i, min, max, sum = 0;
double average;
for (i = 0; i < 3; ++i)
{
cout << "Enter number " << i + 1 << ':';
cin >> *p;
sum += *p++;
}
for (p = m, min = max = *p++, i = 1; i < 3; ++i, ++p)
{
if (*p > max)
max = *p;
if (*p < min)
min = *p;
}
average = sum / 3;
cout << "Sum = " << sum << endl
<< "Min = " << min << endl
<< "Max = " << max << endl
<< "Average = " << average << endl;
system("pause");
}

You might also like