# Largest Divisor of Integer

Page 1 of 1

## 1 Replies - 5542 Views - Last Post: 30 April 2007 - 05:08 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27433&amp;s=0b54ce6dc0e81fef4a5d404d9e83a8f4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 claire_armstrong++

• New D.I.C Head

Reputation: 0
• 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

• g+ + -o drink whiskey.cpp

Reputation: 250
• 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.