From 25cb882f9d3070c6d924598b8e45efe4271e30bd Mon Sep 17 00:00:00 2001 From: aziz Date: Sat, 16 Dec 2023 10:06:29 +0600 Subject: [PATCH] print all divisors efficiently --- Maths/divisors.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Maths/divisors.cpp diff --git a/Maths/divisors.cpp b/Maths/divisors.cpp new file mode 100644 index 00000000..e03bd042 --- /dev/null +++ b/Maths/divisors.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +void printDivisors(long long int n){ + for(long long int i = 1; i * i <= n; ++i){ + if(n % i == 0){ + if(n / i == i) cout << i << ' '; + else cout << i << ' ' << n / i << ' '; + } + } + cout << endl; +} + +int main(void){ + long long int n; + while(1){ + cout << "enter the value of n [n<= 0 to stop]: "; + cin >> n; + if(n <= 0) break; + else printDivisors(n); + } +}