Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-17 10:37:09
forward
1242 people have browsed it

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
Copy after login

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;
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template