• 0

[C++] Find integer with the most divisors. HELP!


Question

So I've been banging my head on this for about two weeks now, and I'm trying to figure out how to get this to work. Whenever I build and execute, it displays 1000 0's. I think you can figure out what I'm trying to achieve, but this is just the basis for the program, the output will be configured later.

What am I doing wrong?

//Finds the integer from 1 to 1000 with the most divisors that produces no remainder.

#include <iostream.h>
#include <math.h>

main()
{
	//Define Variables
	// d is the divisor, v is the numerical value, r is the remainder, n is the number of divisors, and b is a dummy variable.
  
  int d, v, r, n, b;
  r = 0;
  n = 0;
  b = 0;

	//Math/Loop
  for(v = 0; v <= 1000; v++)
 	 {
 	 for(d = 1; d < v; d++)
    {
    r = v % d;
    if(r = 0)
   	 {
   	 n++;
   	 }
    }
 	 cout << r;
 	 }
 	 
 	 
 	 
 	 
	return 0;
}

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

if(r = 0)

That assigns r = 0 and returns false always. You want:

if(r == 0)

If you turn on full warnings most compilers will detect it as an error.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.