C Program to Check if count of divisors is even or odd?

CServer Side ProgrammingProgramming

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\n");
   else
      printf("Odd\n");
   return 0;
}
raja
Published on 20-Aug-2019 12:04:59
Advertisements