#includeusingnamespacest"/> #includeusingnamespacest">

Home  >  Article  >  Backend Development  >  C program to check if the number of divisors is even or odd?

C program to check if the number of divisors is even or odd?

WBOY
WBOYforward
2023-09-17 10:37:091243browse

C program to check if the number of divisors is even or odd?

Given a number "n" as input, this program aims to find whether the total number of divisors of n is even or odd Even numbers are integers divisible by 2. Example: 0, 8, -24

Odd numbers are integers that are not divisible by 2. Example: 1, 7, -11 , 15

Input: 10
Output: Even

Explanation

Find all factors of n and then check whether the total number of factors is even or odd. To do this, find all the factors and calculate the quantity, then divide this quantity 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</p><p>");
   else
      printf("Odd</p><p>");
   return 0;
}

The above is the detailed content of C program to check if the number of divisors is even or odd?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete