
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Numbers with Odd 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