Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote