Print the largest divisor of a number , different then him.

Hy there !

Print the largest divisor of a number , different then him.

E.c: So if I type 12 , it must show me the biggest divisor different then him ,which is 6.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"a=";cin>>a;
int i;
for(i=a/2;i>=1;i--)
if(a%i==0){
    cout<<i;
    break;
}
return 0;
}


P.S: I dont undertand the logic of this code , mostly the "for". Can you give me a different "for" which can give same results,for me to understand better.
I dont understand in "for" why i=a/2 , or why i>=1.

Sry my english, i am middle europe.
Last edited on
You had an example of a==12. i=a/2 means i=6 on first iteration.
12/2==6, 12/6==2, and 2*6==12

If you would take any value larger than a/2, how could it possibly be divisor?

Lets say that x > a/2 and x is divisor. Then a/x < 2. There is only one integer smaller than 2 and that is 1. The a/x==1, if x==a, and that you did not want.

Lets take a=15. a/2==15/2==7. The 7 is not a divisor, and no value larger than 7 is divisor of 15.

a/2 is either the biggest divisor, or larger than the biggest divisor.
There is no need to test any value that is larger than a/2.


Do note that the loop counts downwards: for ( i=a/2; i>=1; i-- )
On every new iteration the i is smaller than on the previous iteration.


The condition is i>=1.
The loop continues as long as the condition is true.
As long as the i is greater or equal to 1.
If you let i become less than 1, the i is 0 (and then negative).
Does it make any sense to divide with 0, or a negative value?
No. The loop has to end after i==1.
preety advance for me to understand , but thank you anyway , maybe in a day I can come and understand better your words , I did understand half , even so , my english is not perfect , so I cannot connect all words and give them a logic

I really appreciate your answer, mostly I wanted to know haw a different "for" will had look , giving same result , cause I think is possible.
Last edited on
So I solved , you helped me a lilttle , Here haw I was thinked it , in case it may be usefull for someone else :

So let me tell you about "for";
[for(i=a/2;i>=1;i--)]

1.So [i=a/2]cause this is haw you search a divisor number , you see if it can give an int number , so if a number is divide with x (or 2 in this case as a default) , is meen that that number is a divisor of him . Exemple 8:2=4 , so 2 is a divisor of 8
6:3=2 , so 3 is a divisor of 6 cause it give an int number (2)

2.Now [i>=1] cause that divisor which we must print (show at the end) is either 1 or bigger then him. So this is like a rule , a default thing , a base one.

3. Now[i--] cause I want to show me the last but one divisor.
For exemple if I type (a=13) in this problem , it will print me 1 cause "13" had only 2 divisor which are 1 and 13 (himself)

Another exemple if I type (a=26) , it will print me 13
Another exemple if I type (a=7) , it will print me 1.


See http://www.cplusplus.com/doc/tutorial/control/ for longer explanation of the for loop.
wish you best , helped me a lot !!!!!!!!!!!!!!!!!!!!!!!
Topic archived. No new replies allowed.