- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program to Check if count of divisors is even or odd?
Given a number “n” as an input, this program is to find the total number of divisors of n is even or odd. An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24
An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15
Input: 10 Output: Even
Explanation
Find all the divisors of the n and then check if the total number of divisors are even or odd. To do this find all divisor and count the number and then divide this number by 2 to check if it is even or odd.
Example
#include <iostream> #include <math.h> using namespace std; int main() { int n=10; int count = 0; for (int i = 1; i <= sqrt(n) + 1; i++) { if (n % i == 0) count += (n / i == i) ? 1 : 2; } if (count % 2 == 0) printf("Even
"); else printf("Odd
"); return 0; }
Advertisements