Hi! i want to make a program that inputs a number k and outputs all the numbers
ID: 3564706 • Letter: H
Question
Hi! i want to make a program that inputs a number k and outputs all the numbers between 1 and hundred that have exactly k divisors, not less not more. Here's what i have now, makes no sense why this shouldn't work, any ideas?
#include <iostream>
int main ()
{
int k;
std::cout << "enter a natural number" << " ";
std::cin >> k;
for(int n=1; n<=100; ++n){
int i = 1;
int sum = 0;
while(n/i != 0 && sum<=k){
if(n%i == 0){
sum+=1;
i+=1;
if(sum == k)
std::cout << n << " ";
}else
++i;
}
}
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main (){
int k;
cout << "enter a natural number" << " ";
cin >> k;
for(int n=1; n<=100; ++n){
int i = 1;
int sum = 0;
while(n/i != 0 && sum<=k){
if(n%i == 0){
sum+=1;
i+=1;
}else{
++i;
}
}
if(sum == k){
cout << n << " ";
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.