Largest Divisor of Integer

Not the integer itself!!

Page 1 of 1

1 Replies - 3320 Views - Last Post: 30 April 2007 - 05:08 AM Rate Topic: -----

#1 claire_armstrong++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-March 07

Largest Divisor of Integer

Posted 30 April 2007 - 03:52 AM

I have a function to find the largest divisor of an user input integer, but it returns the integer itself, which I don't want!!

int largestDivisorOfYear(int year)
{
int divisor = year;
while (year % divisor != 0)
{
divisor--;
}
return divisor;
}

Any help is GREATLY appreciated, thank you!!
Is This A Good Question/Topic? 0
  • +

Replies To: Largest Divisor of Integer

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Largest Divisor of Integer

Posted 30 April 2007 - 05:08 AM

It's doing so because you assign the value of year to the variable divisor - the first attempt to use the modulus results in a return value of 0, because they are the same. The loop is not entered, divisor is never decremented, and the original value of divisor, having been assigned from year, is returned.

I'd suggest starting divisor at a lower value than year.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1