Program to find count of numbers having odd number of divisors in given range in C++
-
10-09-2020 - |
Question
Program to find count of numbers having odd number of divisors in given range in C++
In this tutorial, we will be discussing a program to find the count of numbers having odd number of divisors in a given range.
For this we will be provided with the upper and lower limits of the range. Our task is to calculate and count the number of values having an odd number of divisors.
Example
#include <bits/stdc++.h> using namespace std; //counting the number of values //with odd number of divisors int OddDivCount(int a, int b){ int res = 0; for (int i = a; i <= b; ++i) { int divCount = 0; for (int j = 1; j <= i; ++j) { if (i % j == 0) { ++divCount; } } if (divCount % 2) { ++res; } } return res; } int main(){ int a = 1, b = 10; cout << OddDivCount(a, b) << endl; return 0; }
Output
3
Advertisements
Not affiliated with Tutorialspoint